001    /*
002     * Copyright © 2008, 2012 Pedro Agulló Soliveres.
003     * 
004     * This file is part of DirectJNgine.
005     *
006     * DirectJNgine is free software: you can redistribute it and/or modify
007     * it under the terms of the GNU Lesser General Public License as published by
008     * the Free Software Foundation, either version 3 of the License.
009     *
010     * Commercial use is permitted to the extent that the code/component(s)
011     * do NOT become part of another Open Source or Commercially developed
012     * licensed development library or toolkit without explicit permission.
013     *
014     * DirectJNgine is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017     * GNU Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public License
020     * along with DirectJNgine.  If not, see <http://www.gnu.org/licenses/>.
021     * 
022     * This software uses the ExtJs library (http://extjs.com), which is 
023     * distributed under the GPL v3 license (see http://extjs.com/license).
024     */
025    
026    package com.softwarementors.extjs.djn.router.processor;
027    
028    
029    import java.util.List;
030    
031    import com.softwarementors.extjs.djn.DirectJNgineException;
032    import com.softwarementors.extjs.djn.StringUtils;
033    import com.softwarementors.extjs.djn.api.RegisteredMethod;
034    
035    public class RequestException extends DirectJNgineException {
036      private static final long serialVersionUID = -5221533510455590438L;
037    
038      private RequestException( String message ) {
039        super(message);
040        
041        assert !StringUtils.isEmpty(message);
042      }
043    
044      private RequestException( String message, Throwable cause ) {
045        super(message, cause);
046        
047        assert !StringUtils.isEmpty(message);
048        assert cause != null;
049      }
050    
051      public static RequestException forJsonElementMustBeAJsonArray(String elementName, String jsonString ) {
052        assert !StringUtils.isEmpty(elementName);
053        assert !StringUtils.isEmpty(jsonString);
054        
055        return new RequestException( "The method arguments must be a json array, but it is not. Json=" + jsonString );
056      }
057    
058      public static RequestException forActionNotFound(String actionName) {
059        assert !StringUtils.isEmpty( actionName );
060        
061        return new RequestException( "No action registered as '" + actionName + "'" );
062      }
063    
064      public static RequestException forActionMethodNotFound(String actionName, String methodName) {
065        assert !StringUtils.isEmpty( actionName );
066        assert !StringUtils.isEmpty( methodName );
067    
068        return new RequestException( "No method registered as '" + methodName + "' in action '" + actionName + "'" );
069      }
070    
071      public static RequestException forRequestBatchMustHaveAtLeastOneRequest() {
072        return new RequestException( "A batched request must have at least one request, but the json array had no elements." );
073      }
074    
075      public static RequestException forRequestBatchItemMustBeAValidJsonObject(int itemPosition) {
076        assert itemPosition >= 0;
077        
078        return new RequestException( "Item " + itemPosition + " in the batched request json array is not a valid json object");
079      }
080    
081      public static RequestException forRequestMustBeAValidJsonObjectOrArray() {
082        return new RequestException( "The request must be a valid json object or array");
083      }
084    
085      public static RequestException forRequestFormatNotRecognized() {
086        return new RequestException( "Unable to recognize the request format.");
087      }
088    
089      public static RequestException forJsonElementMustBeANonNullOrEmptyValue(String elementName, Class<?> primitiveType) {
090        assert !StringUtils.isEmpty( elementName);
091        assert primitiveType != null;
092    
093        return new RequestException( "The json '" + elementName + "' element is missing, null or emtpy, or it is not of type " + primitiveType.getName() + ".");
094      }
095    
096      public static RequestException forJsonElementMissing(String elementName) {
097        assert !StringUtils.isEmpty( elementName);
098        
099        return new RequestException( "The json '" + elementName + "' element is missing.");
100      }
101    
102      public static RequestException forWrongMethodArgumentCount(RegisteredMethod method,
103          int realArgumentCount) 
104      {
105        assert method != null;
106        assert realArgumentCount >= 0;
107        
108        int expectedArgumentCount = method.getParameterCount();
109        return new RequestException( "Error attempting to call '" + method.getFullName() + "' (Java method='" + method.getFullJavaMethodName() + "'). Expected '" + expectedArgumentCount + "' arguments, but found '" + realArgumentCount + 
110           "'. Note: this can happen sometimes when passing 'undefined' values or just because the JavaScript call was missing some parameters");
111      }
112    
113      public static RequestException forFormPostMissingParameters(List<String> missingParameters) {
114        assert missingParameters != null;
115        assert !missingParameters.isEmpty();
116        
117        return new RequestException( "Form post request is missing the following parameters: " + StringUtils.concatWithSeparator( missingParameters, ", " ) );
118      }
119    
120      public static RequestException forPollEventNotFound(String eventName) {
121        assert !StringUtils.isEmpty( eventName );
122    
123        return new RequestException( "No method registered for poll event '" + eventName + "'" );
124      }
125    
126      public static RequestException forSourceNotFound( String sourceName) {
127        assert !StringUtils.isEmpty(sourceName);
128        
129        return new RequestException( "Unable to find source for '" + sourceName + "'");
130      }
131    }