- Timestamp:
- 08/18/11 12:39:22 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ether_megapoli/trunk/web/src/com/ether/ControllerEther.java
r89 r130 1 1 package com.ether; 2 2 3 import com.ether.annotation.ControllerMethod; 4 import com.ether.annotation.Mandatory; 5 import com.ether.annotation.ParamName; 6 import com.ether.annotation.UseJSON; 7 import net.sf.json.JSON; 8 import net.sf.json.JSONArray; 9 import net.sf.json.JSONNull; 10 import net.sf.json.JSONObject; 11 import net.sf.json.util.JSONUtils; 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 import org.jetbrains.annotations.NotNull; 15 import org.jetbrains.annotations.Nullable; 16 import org.springframework.beans.factory.annotation.Required; 17 import org.springframework.web.servlet.ModelAndView; 18 import org.springframework.web.servlet.mvc.AbstractController; 19 import org.springframework.web.servlet.mvc.multiaction.MethodNameResolver; 20 import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException; 21 22 import javax.servlet.http.HttpServletRequest; 23 import javax.servlet.http.HttpServletResponse; 3 24 import java.io.UnsupportedEncodingException; 4 25 import java.lang.annotation.Annotation; … … 12 33 import java.util.Map; 13 34 14 import javax.servlet.http.HttpServletRequest; 15 import javax.servlet.http.HttpServletResponse; 16 17 import net.sf.json.JSON; 18 import net.sf.json.JSONArray; 19 import net.sf.json.JSONNull; 20 import net.sf.json.JSONObject; 21 import net.sf.json.util.JSONUtils; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.jetbrains.annotations.NotNull; 26 import org.jetbrains.annotations.Nullable; 27 import org.springframework.beans.factory.annotation.Required; 28 import org.springframework.web.servlet.ModelAndView; 29 import org.springframework.web.servlet.mvc.AbstractController; 30 import org.springframework.web.servlet.mvc.multiaction.MethodNameResolver; 31 import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException; 32 33 import com.ether.annotation.ControllerMethod; 34 import com.ether.annotation.Mandatory; 35 import com.ether.annotation.ParamName; 36 import com.ether.annotation.UseJSON; 37 38 public class ControllerEther extends AbstractController 35 public class ControllerEther 36 extends AbstractController 39 37 { 40 @Override 41 @Nullable 42 protected ModelAndView handleRequestInternal(@NotNull final HttpServletRequest request, @NotNull final HttpServletResponse response) 43 throws WebException 44 { 45 try { 46 if (!_initialized) 47 initialize(); 48 49 request.setCharacterEncoding(UTF8Charset.getEncoding()); 50 51 // Get method to call 52 final String methodName = _methodNameResolver.getHandlerMethodName(request); 53 54 final MethodDescription methodDescription = _methods.get(methodName); 55 if (null == methodDescription) { 56 WebHelper.displayAjaxError(response, methodName); 57 if (LOGGER.isWarnEnabled()) 58 LOGGER.warn(String.format("Controller=%1$s Method=%2$s METHOD NOT FOUND", getClass().getSimpleName(), methodName)); 59 return null; 60 } 61 62 return invokeMethod(methodDescription, request, response); 63 64 } catch (UnsupportedEncodingException e) { 65 throw new WebException(WebException.WebCode.ERROR_UNSUPPORTED_UTF8_ENCODING, e); 66 } catch (NoSuchRequestHandlingMethodException e) { 67 throw new WebException(WebException.WebCode.ERROR_NO_REQUEST_HANDLING_METHOD, e); 68 } 69 } 70 71 @Nullable 72 private ModelAndView invokeMethod( @NotNull final MethodDescription methodDescription, @NotNull final HttpServletRequest request, @NotNull final HttpServletResponse response) 73 throws WebException 74 { 75 try { 76 // Parse parameters 77 final Object[] params = buildParams(methodDescription, request); 78 79 // Invoke method (go to controller) 80 final Object result = methodDescription.getMethod().invoke(this, params); 81 82 // Return result 83 if (!methodDescription.getView().isEmpty()) { 84 response.setStatus(HttpServletResponse.SC_OK); 85 if (result instanceof Map) 86 return new ModelAndView(methodDescription.getView(), (Map) result); 87 else 88 return new ModelAndView(methodDescription.getView(), "result", result); 89 90 } else if (methodDescription.isJsonResult()) { 91 response.setStatus(HttpServletResponse.SC_OK); 92 final String jsonResult = convertToJson(result); 93 WebHelper.writeJsonToResponse(response, jsonResult); 94 return null; 95 } else { 96 response.setStatus(HttpServletResponse.SC_NO_CONTENT); 97 return null; 98 } 99 100 } catch (Throwable e) { 101 throw new WebException(e); 102 } 103 } 104 105 @NotNull 106 private String convertToJson( @Nullable final Object object ) 107 { 108 if( null == object) 38 @Override 39 @Nullable 40 protected ModelAndView handleRequestInternal( @NotNull final HttpServletRequest request, @NotNull final HttpServletResponse response ) 41 throws WebException, EtherException 42 { 43 try 44 { 45 if( !_initialized ) 46 initialize(); 47 48 request.setCharacterEncoding( UTF8Charset.getEncoding() ); 49 50 // Get method to call 51 final String methodName = _methodNameResolver.getHandlerMethodName( request ); 52 53 final MethodDescription methodDescription = _methods.get( methodName ); 54 if( null == methodDescription ) 55 { 56 WebHelper.displayAjaxError( response, methodName ); 57 if( LOGGER.isWarnEnabled() ) 58 LOGGER.warn( String.format( "Controller=%1$s Method=%2$s METHOD NOT FOUND", getClass().getSimpleName(), methodName ) ); 59 return null; 60 } 61 62 return invokeMethod( methodDescription, request, response ); 63 64 } 65 catch( UnsupportedEncodingException e ) 66 { 67 throw new WebException( WebException.WebCode.ERROR_UNSUPPORTED_UTF8_ENCODING, e ); 68 } 69 catch( NoSuchRequestHandlingMethodException e ) 70 { 71 throw new WebException( WebException.WebCode.ERROR_NO_REQUEST_HANDLING_METHOD, e ); 72 } 73 } 74 75 @Nullable 76 private ModelAndView invokeMethod( @NotNull final MethodDescription methodDescription, @NotNull final HttpServletRequest request, @NotNull final HttpServletResponse response ) 77 throws WebException 78 { 79 try 80 { 81 // Parse parameters 82 final Object[] params = buildParams( methodDescription, request ); 83 84 // Invoke method (go to controller) 85 final Object result = methodDescription.getMethod().invoke( this, params ); 86 87 // Return result 88 if( !methodDescription.getView().isEmpty() ) 89 { 90 response.setStatus( HttpServletResponse.SC_OK ); 91 if( result instanceof Map ) 92 return new ModelAndView( methodDescription.getView(), (Map) result ); 93 else 94 return new ModelAndView( methodDescription.getView(), "result", result ); 95 96 } 97 else if( methodDescription.isJsonResult() ) 98 { 99 response.setStatus( HttpServletResponse.SC_OK ); 100 final String jsonResult = convertToJson( result ); 101 WebHelper.writeJsonToResponse( response, jsonResult ); 102 return null; 103 } 104 else 105 { 106 response.setStatus( HttpServletResponse.SC_NO_CONTENT ); 107 return null; 108 } 109 110 } 111 catch( Throwable e ) 112 { 113 throw new WebException( e ); 114 } 115 } 116 117 @NotNull 118 private String convertToJson( @Nullable final Object object ) 119 { 120 if( null == object ) 109 121 return JSONNull.getInstance().toString(); 110 122 if( object instanceof JSON ) … … 119 131 } 120 132 121 @NotNull 122 private Object[] buildParams(@NotNull final MethodDescription methodDescription, @NotNull final HttpServletRequest request) 123 throws InvalidParameterException 124 { 125 final List<Object> params = new ArrayList<Object>(); 126 for (final ParamDescription paramDescription : methodDescription.getParams()) { 127 if (null == paramDescription) { 128 params.add(null); 129 continue; 130 } 131 final boolean canBeNull = !paramDescription.isPrimitive() && !paramDescription.isMandatory(); 132 final String paramValue = WebHelper.getRequestParameter(request, paramDescription.getName(), null, canBeNull); 133 final boolean useJSON = paramDescription.isUsingJSON(); 134 params.add(convertParameter(paramDescription.getType(), paramValue, useJSON)); 135 } 136 return params.toArray(new Object[params.size()]); 137 } 138 139 @Nullable 140 private Object convertParameter( @NotNull final Class paramType, @Nullable final String paramValue, final boolean useJSON ) 133 @NotNull 134 private Object[] buildParams( @NotNull final MethodDescription methodDescription, @NotNull final HttpServletRequest request ) 135 throws InvalidParameterException 136 { 137 final List<Object> params = new ArrayList<Object>(); 138 for( final ParamDescription paramDescription : methodDescription.getParams() ) 139 { 140 if( null == paramDescription ) 141 { 142 params.add( null ); 143 continue; 144 } 145 final boolean canBeNull = !paramDescription.isPrimitive() && !paramDescription.isMandatory(); 146 final String paramValue = WebHelper.getRequestParameter( request, paramDescription.getName(), null, canBeNull ); 147 final boolean useJSON = paramDescription.isUsingJSON(); 148 params.add( convertParameter( paramDescription.getType(), paramValue, useJSON ) ); 149 } 150 return params.toArray( new Object[params.size()] ); 151 } 152 153 @Nullable 154 private Object convertParameter( @NotNull final Class paramType, @Nullable final String paramValue, final boolean useJSON ) 141 155 { 142 156 if( null == paramValue ) … … 148 162 if( JSONArray.class.equals( paramType ) ) 149 163 return _jsonHelper.toJSON( paramValue ); 150 if( String.class 164 if( String.class.equals( paramType ) ) 151 165 return paramValue; 152 166 if( Boolean.TYPE.equals( paramType ) || Boolean.class.equals( paramType ) ) … … 170 184 } 171 185 172 private void initialize() 173 186 private void initialize() 187 throws WebException 174 188 { 175 189 for( final Method method : getClass().getMethods() ) … … 178 192 if( null != annotation ) 179 193 { 180 181 182 194 final MethodDescription methodDescription = new MethodDescription( method, annotation ); 195 fillMethodDescription( method, methodDescription ); 196 _methods.put( method.getName(), methodDescription ); 183 197 } 184 198 } … … 187 201 } 188 202 189 private void fillMethodDescription( @NotNull final Method method, @NotNull final MethodDescription methodDescription ) 190 203 private void fillMethodDescription( @NotNull final Method method, @NotNull final MethodDescription methodDescription ) 204 throws WebException 191 205 { 192 206 final Class<?>[] paramTypes = method.getParameterTypes(); 193 207 final Annotation[][] paramAnnotations = method.getParameterAnnotations(); 194 208 195 if( paramTypes.length != paramAnnotations.length)196 throw new WebException(WebException.WebCode.ERROR_NUMBER_OF_PARAM_TYPES_NOT_EQUAL_TO_PARAM_ANNOTATIONS, paramTypes.length+" "+paramAnnotations.length);197 209 if( paramTypes.length != paramAnnotations.length ) 210 throw new WebException( WebException.WebCode.ERROR_NUMBER_OF_PARAM_TYPES_NOT_EQUAL_TO_PARAM_ANNOTATIONS, paramTypes.length + " " + paramAnnotations.length ); 211 198 212 for( int i = 0; i < paramTypes.length; ++i ) 199 213 { … … 219 233 } 220 234 } 221 } 222 223 235 } 236 237 224 238 @Required 225 239 public void setMethodNameResolver( final MethodNameResolver methodNameResolver ) … … 236 250 public void setJsonHelper( final JSONHelper jsonHelper ) 237 251 { 238 252 _jsonHelper = jsonHelper; 239 253 } 240 254 … … 245 259 246 260 private static final Log LOGGER = LogFactory.getLog( ControllerEther.class ); 247 261 248 262 private JSONHelper _jsonHelper; 249 263 private MethodNameResolver _methodNameResolver;
Note: See TracChangeset
for help on using the changeset viewer.