Ignore:
Timestamp:
08/18/11 12:39:22 (13 years ago)
Author:
vmipsl
Message:

[Visualization] interface quite finish

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ether_megapoli/trunk/web/src/com/ether/ControllerEther.java

    r89 r130  
    11package com.ether; 
    22 
     3import com.ether.annotation.ControllerMethod; 
     4import com.ether.annotation.Mandatory; 
     5import com.ether.annotation.ParamName; 
     6import com.ether.annotation.UseJSON; 
     7import net.sf.json.JSON; 
     8import net.sf.json.JSONArray; 
     9import net.sf.json.JSONNull; 
     10import net.sf.json.JSONObject; 
     11import net.sf.json.util.JSONUtils; 
     12import org.apache.commons.logging.Log; 
     13import org.apache.commons.logging.LogFactory; 
     14import org.jetbrains.annotations.NotNull; 
     15import org.jetbrains.annotations.Nullable; 
     16import org.springframework.beans.factory.annotation.Required; 
     17import org.springframework.web.servlet.ModelAndView; 
     18import org.springframework.web.servlet.mvc.AbstractController; 
     19import org.springframework.web.servlet.mvc.multiaction.MethodNameResolver; 
     20import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException; 
     21 
     22import javax.servlet.http.HttpServletRequest; 
     23import javax.servlet.http.HttpServletResponse; 
    324import java.io.UnsupportedEncodingException; 
    425import java.lang.annotation.Annotation; 
     
    1233import java.util.Map; 
    1334 
    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 
     35public class ControllerEther 
     36        extends AbstractController 
    3937{ 
    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 ) 
    109121            return JSONNull.getInstance().toString(); 
    110122        if( object instanceof JSON ) 
     
    119131    } 
    120132 
    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 ) 
    141155    { 
    142156        if( null == paramValue ) 
     
    148162        if( JSONArray.class.equals( paramType ) ) 
    149163            return _jsonHelper.toJSON( paramValue ); 
    150         if( String.class .equals( paramType ) ) 
     164        if( String.class.equals( paramType ) ) 
    151165            return paramValue; 
    152166        if( Boolean.TYPE.equals( paramType ) || Boolean.class.equals( paramType ) ) 
     
    170184    } 
    171185 
    172     private void initialize()  
    173         throws WebException 
     186    private void initialize() 
     187            throws WebException 
    174188    { 
    175189        for( final Method method : getClass().getMethods() ) 
     
    178192            if( null != annotation ) 
    179193            { 
    180                 final MethodDescription methodDescription = new MethodDescription( method, annotation ); 
    181                 fillMethodDescription( method, methodDescription ); 
    182                 _methods.put( method.getName(), methodDescription ); 
     194                final MethodDescription methodDescription = new MethodDescription( method, annotation ); 
     195                fillMethodDescription( method, methodDescription ); 
     196                _methods.put( method.getName(), methodDescription ); 
    183197            } 
    184198        } 
     
    187201    } 
    188202 
    189     private void fillMethodDescription( @NotNull final Method method, @NotNull final MethodDescription methodDescription )  
    190         throws WebException 
     203    private void fillMethodDescription( @NotNull final Method method, @NotNull final MethodDescription methodDescription ) 
     204            throws WebException 
    191205    { 
    192206        final Class<?>[] paramTypes = method.getParameterTypes(); 
    193207        final Annotation[][] paramAnnotations = method.getParameterAnnotations(); 
    194208 
    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 
    198212        for( int i = 0; i < paramTypes.length; ++i ) 
    199213        { 
     
    219233            } 
    220234        } 
    221     }     
    222  
    223      
     235    } 
     236 
     237 
    224238    @Required 
    225239    public void setMethodNameResolver( final MethodNameResolver methodNameResolver ) 
     
    236250    public void setJsonHelper( final JSONHelper jsonHelper ) 
    237251    { 
    238         _jsonHelper = jsonHelper; 
     252        _jsonHelper = jsonHelper; 
    239253    } 
    240254 
     
    245259 
    246260    private static final Log LOGGER = LogFactory.getLog( ControllerEther.class ); 
    247     
     261 
    248262    private JSONHelper _jsonHelper; 
    249263    private MethodNameResolver _methodNameResolver; 
Note: See TracChangeset for help on using the changeset viewer.