<commit_before>/*
 * $Id$
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.googlcode.strut2gwtplugin.interceptor;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.ServletContext;

import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.server.rpc.RPC;
import com.google.gwt.user.server.rpc.RPCRequest;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.opensymphony.xwork2.ActionInvocation;

/**
 * This class is a modified version of GWT's RemoteServiceServlet.java
 */
class GWTServlet extends RemoteServiceServlet {

    /** Context for the servlet */
    private ServletContext servletContext;
    
    /** Action class to invoke */
    private ActionInvocation actionInvocation;

    /**
     * Find the invoked method on either the specified interface or any super.
     */
    private static Method findInterfaceMethod(Class intf, String methodName,
        Class[] paramTypes, boolean includeInherited) {
        try {
            return intf.getDeclaredMethod(methodName, paramTypes);
        } catch(NoSuchMethodException e) {
            if(includeInherited) {
                Class[] superintfs = intf.getInterfaces();
                for(int i = 0; i < superintfs.length; i++) {
                    Method method = findInterfaceMethod(superintfs[i],
                        methodName, paramTypes, true);
                    if(method != null) {
                        return method;
                    }
                }
            }

            return null;
        }
    }

    /**
     * The default constructor.
     */
    public GWTServlet() {
        super();
    }

    /* (non-Javadoc)
     * @see com.google.gwt.user.server.rpc.RemoteServiceServlet#processCall(java.lang.String)
     */
    public String processCall(String payload) throws SerializationException {
        
        // default return value - Should this be something else?
        // no known constants to use in this case
        String result = null; 
        
        // get the RPC Request from the request data
        RPCRequest rpcRequest= RPC.decodeRequest(payload);
        
        // get the parameter types for the method look-up
        Class[] paramTypes = new Class[rpcRequest.getParameters().length];
        for (int i=0; i < paramTypes.length; i++) {
            paramTypes[i] = rpcRequest.getParameters()[i].getClass();
        }
        
        // we need to get the action method from Struts
        Method method = findInterfaceMethod(
                actionInvocation.getAction().getClass(), 
                rpcRequest.getMethod().getName(), 
                paramTypes, true);
        
        // if the method is null, this may be a hack attempt
        // or we have some other big problem
        if (method == null) {
            // present the params
            StringBuffer params = new StringBuffer();
            for (int i=0; i < paramTypes.length; i++) {
                params.append(paramTypes[i]);
                if (i < paramTypes.length-1) {
                    params.append(", ");
                }
            }
            
            // throw a security exception, could be attempted hack
            //
            // XXX - should we package up the failure like we do below? 
            //
            throw new SecurityException(
                    "Failed to locate method "+ rpcRequest.getMethod().getName()
                    + "("+ params +") on interface "
                    + actionInvocation.getAction().getClass().getName()
                    + " requested through interface "
                    + rpcRequest.getClass().getName());
        }
        
        // now make the call
        Object callResult = null;
        try {
            callResult = method.invoke(actionInvocation.getAction(), 
                    rpcRequest.getParameters());
        } catch (IllegalAccessException iie) {
            // This may need to change this to package up up the cause
            // instead of the thrown exception, not sure if the 
            // chaining will translate
            result = RPC.encodeResponseForFailure(method, iie);
        } catch (InvocationTargetException ite) {
            // This may need to change this to package up up the cause
            // instead of the thrown exception, not sure if the 
            // chaining will translate
            result = RPC.encodeResponseForFailure(method, ite);
        }
        
        // if we got a respone, package it up for GWT
        if (callResult != null) {
            result = RPC.encodeResponseForSuccess(method, callResult);
        }
        
        // return our response<commit_msg>Fix for Issue 3: - Encode null response for void actions or those which return null values<commit_after>/*
 * $Id$
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.googlcode.strut2gwtplugin.interceptor;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.ServletContext;

import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.server.rpc.RPC;
import com.google.gwt.user.server.rpc.RPCRequest;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.opensymphony.xwork2.ActionInvocation;

/**
 * This class is a modified version of GWT's RemoteServiceServlet.java
 */
class GWTServlet extends RemoteServiceServlet {

    /** Context for the servlet */
    private ServletContext servletContext;
    
    /** Action class to invoke */
    private ActionInvocation actionInvocation;

    /**
     * Find the invoked method on either the specified interface or any super.
     */
    private static Method findInterfaceMethod(Class intf, String methodName,
        Class[] paramTypes, boolean includeInherited) {
        try {
            return intf.getDeclaredMethod(methodName, paramTypes);
        } catch(NoSuchMethodException e) {
            if(includeInherited) {
                Class[] superintfs = intf.getInterfaces();
                for(int i = 0; i < superintfs.length; i++) {
                    Method method = findInterfaceMethod(superintfs[i],
                        methodName, paramTypes, true);
                    if(method != null) {
                        return method;
                    }
                }
            }

            return null;
        }
    }

    /**
     * The default constructor.
     */
    public GWTServlet() {
        super();
    }

    /* (non-Javadoc)
     * @see com.google.gwt.user.server.rpc.RemoteServiceServlet#processCall(java.lang.String)
     */
    public String processCall(String payload) throws SerializationException {
        
        // default return value - Should this be something else?
        // no known constants to use in this case
        String result = null; 
        
        // get the RPC Request from the request data
        RPCRequest rpcRequest= RPC.decodeRequest(payload);
        
        // get the parameter types for the method look-up
        Class[] paramTypes = new Class[rpcRequest.getParameters().length];
        for (int i=0; i < paramTypes.length; i++) {
            paramTypes[i] = rpcRequest.getParameters()[i].getClass();
        }
        
        // we need to get the action method from Struts
        Method method = findInterfaceMethod(
                actionInvocation.getAction().getClass(), 
                rpcRequest.getMethod().getName(), 
                paramTypes, true);
        
        // if the method is null, this may be a hack attempt
        // or we have some other big problem
        if (method == null) {
            // present the params
            StringBuffer params = new StringBuffer();
            for (int i=0; i < paramTypes.length; i++) {
                params.append(paramTypes[i]);
                if (i < paramTypes.length-1) {
                    params.append(", ");
                }
            }
            
            // throw a security exception, could be attempted hack
            //
            // XXX - should we package up the failure like we do below? 
            //
            throw new SecurityException(
                    "Failed to locate method "+ rpcRequest.getMethod().getName()
                    + "("+ params +") on interface "
                    + actionInvocation.getAction().getClass().getName()
                    + " requested through interface "
                    + rpcRequest.getClass().getName());
        }
        
        // now make the call
        Object callResult = null;
        try {
            callResult = method.invoke(actionInvocation.getAction(), 
                    rpcRequest.getParameters());
        } catch (IllegalAccessException iie) {
            // This may need to change this to package up up the cause
            // instead of the thrown exception, not sure if the 
            // chaining will translate
            result = RPC.encodeResponseForFailure(method, iie);
        } catch (InvocationTargetException ite) {
            // This may need to change this to package up up the cause
            // instead of the thrown exception, not sure if the 
            // chaining will translate
            result = RPC.encodeResponseForFailure(method, ite);
        }
        
        // package  up response for GWT
        result = RPC.encodeResponseForSuccess(method, callResult);
        
        // return our response
{'fraction_non_alphanumeric': 0.08426303854875283, 'fraction_numerical': 0.0019047619047619048, 'mean_word_length': 1.9735706580366774, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4443019', 'n_tokens_mistral': 2753, 'n_tokens_neox': 2681, 'n_words': 916}