001    /*
002     * Copyright © 2008, 2009 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 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 General Public License for more details.
018     *
019     * You should have received a copy of the GNU 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.lang.reflect.Method;
030    import java.util.List;
031    
032    import com.softwarementors.extjs.djn.DirectJNgineException;
033    import com.softwarementors.extjs.djn.StringUtils;
034    
035    public class RequestException extends DirectJNgineException {
036      private RequestException( String message ) {
037        super(message);
038        
039        assert !StringUtils.isEmpty(message);
040      }
041    
042      private RequestException( String message, Throwable cause ) {
043        super(message, cause);
044        
045        assert !StringUtils.isEmpty(message);
046        assert cause != null;
047      }
048    
049      public static RequestException forJsonElementMustBeAJsonArray(String elementName, String jsonString ) {
050        assert !StringUtils.isEmpty(elementName);
051        assert !StringUtils.isEmpty(jsonString);
052        
053        return new RequestException( "The method arguments must be a json array, but it is not. Json=" + jsonString );
054      }
055    
056      public static RequestException forActionNotFound(String actionName) {
057        assert !StringUtils.isEmpty( actionName );
058        
059        return new RequestException( "No action registered as '" + actionName + "'" );
060      }
061    
062      public static RequestException forActionMethodNotFound(String actionName, String methodName) {
063        assert !StringUtils.isEmpty( actionName );
064        assert !StringUtils.isEmpty( methodName );
065    
066        return new RequestException( "No method registered as '" + methodName + "' in action '" + actionName + "'" );
067      }
068    
069      public static RequestException forRequestBatchMustHaveAtLeastOneRequest() {
070        return new RequestException( "A batched request must have at least one request, but the json array had no elements." );
071      }
072    
073      public static RequestException forRequestBatchItemMustBeAValidJsonObject(int itemPosition) {
074        assert itemPosition >= 0;
075        
076        return new RequestException( "Item " + itemPosition + " in the batched request json array is not a valid json object");
077      }
078    
079      public static RequestException forRequestMustBeAValidJsonObjectOrArray() {
080        return new RequestException( "The request must be a valid json object or array");
081      }
082    
083      public static RequestException forRequestFormatNotRecognized() {
084        return new RequestException( "Unable to recognize the request format.");
085      }
086    
087      public static RequestException forJsonElementMustBeANonNullOrEmptyValue(String elementName, Class<?> primitiveType) {
088        assert !StringUtils.isEmpty( elementName);
089        assert primitiveType != null;
090    
091        return new RequestException( "The json '" + elementName + "' element is missing, null or emtpy, or it is not of type " + primitiveType.getName() + ".");
092      }
093    
094      public static RequestException forJsonElementMissing(String elementName) {
095        assert !StringUtils.isEmpty( elementName);
096        
097        return new RequestException( "The json '" + elementName + "' element is missing.");
098      }
099    
100      public static RequestException forWrongMethodArgumentCount(Method method,
101          int expectedArgumentCount, int realArgumentCount) {
102        assert expectedArgumentCount >= 0;
103        assert realArgumentCount >= 0;
104        
105        return new RequestException( "Wrong call for method '" + method.getName() + "'. Expected '" + expectedArgumentCount + "' arguments, but found '" + realArgumentCount + "'. Note: this can happen sometimes when passing 'undefined' values or just because the JavaScript call was missing some parameters");
106      }
107    
108      public static RequestException forFormPostMissingParameters(List<String> missingParameters) {
109        assert missingParameters != null;
110        assert !missingParameters.isEmpty();
111        
112        return new RequestException( "Form post request is missing the following parameters: " + StringUtils.concatWithSeparator( missingParameters, ", " ) );
113      }
114    
115      public static RequestException forPollEventNotFound(String eventName) {
116        assert !StringUtils.isEmpty( eventName );
117    
118        return new RequestException( "No method registered for poll event '" + eventName + "'" );
119      }
120    
121    }