<commit_before>package org.appwork.remotecall.server;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;

import org.appwork.remotecall.RemoteCallInterface;
import org.appwork.remotecall.ResponseAlreadySentException;
import org.appwork.remotecall.Utils;
import org.appwork.remotecall.client.MethodHandler;
import org.appwork.storage.JSonStorage;
import org.appwork.storage.TypeRef;
import org.appwork.utils.logging.Log;

public class RemoteCallServer {

    private final HashMap<String, RemoteCallServiceWrapper> servicesMap;

    public RemoteCallServer() {

        this.servicesMap = new HashMap<String, RemoteCallServiceWrapper>();
    }

    public <T extends RemoteCallInterface> void addHandler(final Class<T> class1, final T serviceImpl) throws ParsingException {

        if (this.servicesMap.containsKey(class1.getSimpleName())) { throw new IllegalArgumentException("Service " + class1 + " already exists"); }
        this.servicesMap.put(class1.getSimpleName(), new RemoteCallServiceWrapper(class1, serviceImpl));
    }

    /**
     * IMPORTANT: parameters must be urldecoded!!
     * 
     * @param requestor
     * @param clazz
     * @param method
     * @param parameters
     * @return
     * @throws ServerInvokationException
     */
    protected String handleRequest(final Requestor requestor, final String clazz, final String method, final String[] parameters) throws ServerInvokationException {
        try {
            final RemoteCallServiceWrapper service = this.servicesMap.get(new String(clazz));
            if (service == null) { //
                throw new ServerInvokationException(this.handleRequestError(requestor, new BadRequestException("Service not defined: " + clazz)), requestor);

            }
            // find method

            final MethodHandler m = service.getHandler(method);
            if (m == null) { throw new ServerInvokationException(this.handleRequestError(requestor, new BadRequestException("Routine not defined: " + method)), requestor); }

            final TypeRef<Object>[] types = m.getTypeRefs();
            if (types.length != parameters.length) { throw new ServerInvokationException(this.handleRequestError(requestor, new BadRequestException("parameters did not match " + method)), requestor); }

            final Object[] params = new Object[types.length];
            try {
                for (int i = 0; i < types.length; i++) {
                    // parameters should already be urldecoded here

                    if (types[i].getType() == String.class && !"null".equals(parameters[i])) {
                        // fix if there is no " around strings
                        if (!parameters[i].startsWith("\"")) {
                            parameters[i] = "\"" + parameters[i];
                        }
                        if (!parameters[i].endsWith("\"") || parameters[i].length() == 1) {
                            parameters[i] += "\"";
                        }
                    }
                    params[i] = Utils.convert(JSonStorage.restoreFromString(parameters[i], types[i]), types[i].getType());
                }

            } catch (final Exception e) {
                throw new ServerInvokationException(this.handleRequestError(requestor, new BadRequestException("Parameter deserialize error for " + method)), requestor);
            }

            Object answer;

            answer = service.call(m.getMethod(), params);
            return JSonStorage.serializeToJson(answer);

        } catch (final InvocationTargetException e1) {
            final Throwable cause = e1.getCause();
            if (cause != null) {
                if (cause instanceof ResponseAlreadySentException) { throw (ResponseAlreadySentException) cause; }
                throw new ServerInvokationException(this.handleRequestError(requestor, cause), requestor);
            }
            throw new ServerInvokationException(this.handleRequestError(requestor, new RuntimeException(e1)), requestor);
        } catch (final ServerInvokationException e) {
            throw e;
        } catch (final Throwable e) {
            throw new ServerInvokationException(this.handleRequestError(requestor, e), requestor);
        }

    }
<commit_msg>Reimplemented Requestor Parameters for remotecall<commit_after>package org.appwork.remotecall.server;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;

import org.appwork.remotecall.RemoteCallInterface;
import org.appwork.remotecall.ResponseAlreadySentException;
import org.appwork.remotecall.Utils;
import org.appwork.remotecall.client.MethodHandler;
import org.appwork.storage.JSonStorage;
import org.appwork.storage.TypeRef;
import org.appwork.utils.logging.Log;

public class RemoteCallServer {

    private final HashMap<String, RemoteCallServiceWrapper> servicesMap;

    public RemoteCallServer() {

        this.servicesMap = new HashMap<String, RemoteCallServiceWrapper>();
    }

    public <T extends RemoteCallInterface> void addHandler(final Class<T> class1, final T serviceImpl) throws ParsingException {

        if (this.servicesMap.containsKey(class1.getSimpleName())) { throw new IllegalArgumentException("Service " + class1 + " already exists"); }
        this.servicesMap.put(class1.getSimpleName(), new RemoteCallServiceWrapper(class1, serviceImpl));
    }

    /**
     * IMPORTANT: parameters must be urldecoded!!
     * 
     * @param requestor
     * @param clazz
     * @param method
     * @param parameters
     * @return
     * @throws ServerInvokationException
     */
    protected String handleRequest(final Requestor requestor, final String clazz, final String method, final String[] parameters) throws ServerInvokationException {
        try {
            final RemoteCallServiceWrapper service = this.servicesMap.get(new String(clazz));
            if (service == null) { //
                throw new ServerInvokationException(this.handleRequestError(requestor, new BadRequestException("Service not defined: " + clazz)), requestor);

            }
            // find method

            final MethodHandler m = service.getHandler(method);
            if (m == null) { throw new ServerInvokationException(this.handleRequestError(requestor, new BadRequestException("Routine not defined: " + method)), requestor); }

            final TypeRef<Object>[] types = m.getTypeRefs();
            if (types.length != parameters.length) { throw new ServerInvokationException(this.handleRequestError(requestor, new BadRequestException("parameters did not match " + method)), requestor); }

            final Object[] params = new Object[types.length];
            try {
                for (int i = 0; i < types.length; i++) {
                    // parameters should already be urldecoded here
                    if (types[i].getType() == Requestor.class) {
                        params[i] = requestor;
                    } else {
                        if (types[i].getType() == String.class && !"null".equals(parameters[i])) {
                            // fix if there is no " around strings
                            if (!parameters[i].startsWith("\"")) {
                                parameters[i] = "\"" + parameters[i];
                            }
                            if (!parameters[i].endsWith("\"") || parameters[i].length() == 1) {
                                parameters[i] += "\"";
                            }
                        }
                        params[i] = Utils.convert(JSonStorage.restoreFromString(parameters[i], types[i]), types[i].getType());
                    }
                }

            } catch (final Exception e) {
                throw new ServerInvokationException(this.handleRequestError(requestor, new BadRequestException("Parameter deserialize error for " + method)), requestor);
            }

            Object answer;

            answer = service.call(m.getMethod(), params);
            return JSonStorage.serializeToJson(answer);

        } catch (final InvocationTargetException e1) {
            Log.exception(e1);
            final Throwable cause = e1.getCause();
            if (cause != null) {
                if (cause instanceof ResponseAlreadySentException) { throw (ResponseAlreadySentException) cause; }
                throw new ServerInvokationException(this.handleRequestError(requestor, cause), requestor);
            }
            throw new ServerInvokationException(this.handleRequestError(requestor, new RuntimeException(e1)), requestor);
        } catch (final ServerInvokationException e) {
            throw e;
        } catch (final Throwable e) {
            throw new ServerInvokationException(this.handleRequestError(requestor, e), requestor);
        }

    }

{'fraction_non_alphanumeric': 0.10158296321603462, 'fraction_numerical': 0.0023915271609156134, 'mean_word_length': 2.473892405063291, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2888100', 'n_tokens_mistral': 2237, 'n_tokens_neox': 2155, 'n_words': 515}