<commit_before>package org.deegree.securityproxy.wps.request;

import static java.lang.String.format;
import static java.util.Arrays.asList;
import static org.deegree.securityproxy.request.GetOwsRequestParserUtils.checkRequiredParameter;
import static org.deegree.securityproxy.request.GetOwsRequestParserUtils.checkSingleRequiredParameter;
import static org.deegree.securityproxy.request.GetOwsRequestParserUtils.evaluateVersion;
import static org.deegree.securityproxy.request.KvpNormalizer.normalizeKvpMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.deegree.securityproxy.request.OwsRequestParser;
import org.deegree.securityproxy.request.OwsServiceVersion;
import org.deegree.securityproxy.request.UnsupportedRequestTypeException;

import com.vividsolutions.jts.geom.Envelope;

/**
 * Parses an incoming {@link javax.servlet.http.HttpServletRequest} into a {@link WpsRequest}.
 * 
 * @author <a href="<EMAIL>"><NAME></a>
 * @author last edited by: $Author: stenger $
 * @version $Revision: $, $Date: $
 */
public class WpsRequestParser implements OwsRequestParser {

    public static final OwsServiceVersion VERSION_100 = new OwsServiceVersion( 1, 0, 0 );

    public static final String WPS_SERVICE = "WPS";

    public static final String GETCAPABILITIES = "GetCapabilities";

    public static final String DESCRIBEPROCESS = "DescribeProcess";

    public static final String EXECUTE = "Execute";

    private static final String REQUEST = "request";

    private static final String SERVICE = "service";

    private static final String VERSION = "version";

    private static final String IDENTIFIER = "identifier";

    private final List<OwsServiceVersion> supportedVersion = asList( VERSION_100 );

    @Override
    @SuppressWarnings("unchecked")
    public WpsRequest parse( HttpServletRequest request )
                            throws UnsupportedRequestTypeException {
        if ( request == null )
            throw new IllegalArgumentException( "Request must not be null!" );
        String serviceName = evaluateServiceName( request );
        Map<String, String[]> normalizedParameterMap = normalizeKvpMap( request.getParameterMap() );
        checkParameters( normalizedParameterMap );
        return parseRequest( serviceName, normalizedParameterMap );
    }

    private WpsRequest parseRequest( String serviceName, Map<String, String[]> normalizedParameterMap )
                            throws UnsupportedRequestTypeException {
        String type = normalizedParameterMap.get( REQUEST )[0];
        if ( GETCAPABILITIES.equalsIgnoreCase( type ) )
            return parseGetCapabilitiesRequest( serviceName, normalizedParameterMap );
        if ( DESCRIBEPROCESS.equalsIgnoreCase( type ) )
            return parseDescribeProcessRequest( serviceName, normalizedParameterMap );
        if ( EXECUTE.equalsIgnoreCase( type ) )
            return parseExecuteRequest( serviceName, normalizedParameterMap );
        throw new IllegalArgumentException( "Unrecognized operation type: " + type );
    }

    private WpsRequest parseGetCapabilitiesRequest( String serviceName, Map<String, String[]> normalizedParameterMap )
                            throws UnsupportedRequestTypeException {
        checkGetCapabilitiesParameters( normalizedParameterMap );
        OwsServiceVersion version = evaluateVersion( VERSION, normalizedParameterMap, supportedVersion );
        return new WpsRequest( GETCAPABILITIES, version, serviceName );
    }

    private WpsRequest parseDescribeProcessRequest( String serviceName, Map<String, String[]> normalizedParameterMap ) {
        checkDescribeProcessParameters( normalizedParameterMap );
        OwsServiceVersion version = evaluateVersion( VERSION, normalizedParameterMap, supportedVersion );
        List<String> extractedIdentifiers = extractIdentifier( normalizedParameterMap.get( IDENTIFIER ) );
        return new WpsRequest( DESCRIBEPROCESS, version, serviceName, extractedIdentifiers );
    }

    private WpsRequest parseExecuteRequest( String serviceName, Map<String, String[]> normalizedParameterMap ) {
        checkExecuteParameters( normalizedParameterMap );
        OwsServiceVersion version = evaluateVersion( VERSION, normalizedParameterMap, supportedVersion );
        List<String> extractedIdentifier = extractIdentifier( normalizedParameterMap.get( IDENTIFIER ) );
        return new WpsRequest( EXECUTE, version, serviceName, extractedIdentifier );
    }

    private String evaluateServiceName( HttpServletRequest request ) {
        String servletPath = request.getServletPath();
        if ( servletPath == null )
            throw new IllegalArgumentException( "Service name must not be null!" );
        if ( servletPath.contains( "/" ) ) {
            String[] splittedServletPath = servletPath.split( "/" );
            return splittedServletPath[splittedServletPath.length - 1];
        }
        return servletPath;
    }

    private void checkParameters( Map<String, String[]> normalizedParameterMap ) {
        checkRequestParameter( normalizedParameterMap );
    }

    private void checkServiceParameter( Map<String, String[]> normalizedParameterMap )
                            throws UnsupportedRequestTypeException {
        String serviceType = checkSingleRequiredParameter( normalizedParameterMap, SERVICE );
        if ( !"wps".equalsIgnoreCase( serviceType ) ) {
            String msg = "Request must contain a \"service\" parameter with value \"wps\"";
            throw new UnsupportedRequestTypeException( msg );
        }
    }

    private void checkRequestParameter( Map<String, String[]> normalizedParameterMap ) {
        checkSingleRequiredParameter( normalizedParameterMap, REQUEST );
    }

    private void checkDescribeProcessParameters( Map<String, String[]> normalizedParameterMap ) {
        checkIdentifiersParameter( normalizedParameterMap );
    }

    private void checkExecuteParameters( Map<String, String[]> normalizedParameterMap ) {
        checkIdentifierParameter( normalizedParameterMap );
    }

    private void checkGetCapabilitiesParameters( Map<String, String[]> normalizedParameterMap )
                            throws UnsupportedRequestTypeException {
        checkServiceParameter( normalizedParameterMap );
    }

    private void checkIdentifiersParameter( Map<String, String[]> normalizedParameterMap ) {
        checkRequiredParameter( normalizedParameterMap, IDENTIFIER );
    }

    private void checkIdentifierParameter( Map<String, String[]> normalizedParameterMap ) {
        checkSingleRequiredParameter( normalizedParameterMap, IDENTIFIER );
    }

    private List<String> extractIdentifier( String[] identifierParameters ) {<commit_msg>Moved check of service parameter to all request types in WpsRequestParser<commit_after>package org.deegree.securityproxy.wps.request;

import org.deegree.securityproxy.request.OwsRequestParser;
import org.deegree.securityproxy.request.OwsServiceVersion;
import org.deegree.securityproxy.request.UnsupportedRequestTypeException;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static java.util.Arrays.asList;
import static org.deegree.securityproxy.request.GetOwsRequestParserUtils.checkRequiredParameter;
import static org.deegree.securityproxy.request.GetOwsRequestParserUtils.checkSingleRequiredParameter;
import static org.deegree.securityproxy.request.GetOwsRequestParserUtils.evaluateVersion;
import static org.deegree.securityproxy.request.KvpNormalizer.normalizeKvpMap;

/**
 * Parses an incoming {@link javax.servlet.http.HttpServletRequest} into a {@link WpsRequest}.
 * 
 * @author <a href="<EMAIL>"><NAME></a>
 * @author last edited by: $Author: stenger $
 * @version $Revision: $, $Date: $
 */
public class WpsRequestParser implements OwsRequestParser {

    public static final OwsServiceVersion VERSION_100 = new OwsServiceVersion( 1, 0, 0 );

    public static final String WPS_SERVICE = "WPS";

    public static final String GETCAPABILITIES = "GetCapabilities";

    public static final String DESCRIBEPROCESS = "DescribeProcess";

    public static final String EXECUTE = "Execute";

    private static final String REQUEST = "request";

    private static final String SERVICE = "service";

    private static final String VERSION = "version";

    private static final String IDENTIFIER = "identifier";

    private final List<OwsServiceVersion> supportedVersion = asList( VERSION_100 );

    @Override
    @SuppressWarnings("unchecked")
    public WpsRequest parse( HttpServletRequest request )
                    throws UnsupportedRequestTypeException {
        if ( request == null )
            throw new IllegalArgumentException( "Request must not be null!" );
        String serviceName = evaluateServiceName( request );
        Map<String, String[]> normalizedParameterMap = normalizeKvpMap( request.getParameterMap() );
        checkParameters( normalizedParameterMap );
        return parseRequest( serviceName, normalizedParameterMap );
    }

    private WpsRequest parseRequest( String serviceName, Map<String, String[]> normalizedParameterMap )
                    throws UnsupportedRequestTypeException {
        String type = normalizedParameterMap.get( REQUEST )[0];
        if ( GETCAPABILITIES.equalsIgnoreCase( type ) )
            return parseGetCapabilitiesRequest( serviceName, normalizedParameterMap );
        if ( DESCRIBEPROCESS.equalsIgnoreCase( type ) )
            return parseDescribeProcessRequest( serviceName, normalizedParameterMap );
        if ( EXECUTE.equalsIgnoreCase( type ) )
            return parseExecuteRequest( serviceName, normalizedParameterMap );
        throw new IllegalArgumentException( "Unrecognized operation type: " + type );
    }

    private WpsRequest parseGetCapabilitiesRequest( String serviceName, Map<String, String[]> normalizedParameterMap )
                    throws UnsupportedRequestTypeException {
        OwsServiceVersion version = evaluateVersion( VERSION, normalizedParameterMap, supportedVersion );
        return new WpsRequest( GETCAPABILITIES, version, serviceName );
    }

    private WpsRequest parseDescribeProcessRequest( String serviceName, Map<String, String[]> normalizedParameterMap ) {
        checkDescribeProcessParameters( normalizedParameterMap );
        OwsServiceVersion version = evaluateVersion( VERSION, normalizedParameterMap, supportedVersion );
        List<String> extractedIdentifiers = extractIdentifier( normalizedParameterMap.get( IDENTIFIER ) );
        return new WpsRequest( DESCRIBEPROCESS, version, serviceName, extractedIdentifiers );
    }

    private WpsRequest parseExecuteRequest( String serviceName, Map<String, String[]> normalizedParameterMap ) {
        checkExecuteParameters( normalizedParameterMap );
        OwsServiceVersion version = evaluateVersion( VERSION, normalizedParameterMap, supportedVersion );
        List<String> extractedIdentifier = extractIdentifier( normalizedParameterMap.get( IDENTIFIER ) );
        return new WpsRequest( EXECUTE, version, serviceName, extractedIdentifier );
    }

    private String evaluateServiceName( HttpServletRequest request ) {
        String servletPath = request.getServletPath();
        if ( servletPath == null )
            throw new IllegalArgumentException( "Service name must not be null!" );
        if ( servletPath.contains( "/" ) ) {
            String[] splittedServletPath = servletPath.split( "/" );
            return splittedServletPath[splittedServletPath.length - 1];
        }
        return servletPath;
    }

    private void checkParameters( Map<String, String[]> normalizedParameterMap )
                    throws UnsupportedRequestTypeException {
        checkServiceParameter( normalizedParameterMap );
        checkRequestParameter( normalizedParameterMap );
    }

    private void checkServiceParameter( Map<String, String[]> normalizedParameterMap )
                    throws UnsupportedRequestTypeException {
        String serviceType = checkSingleRequiredParameter( normalizedParameterMap, SERVICE );
        if ( !"wps".equalsIgnoreCase( serviceType ) ) {
            String msg = "Request must contain a \"service\" parameter with value \"wps\"";
            throw new UnsupportedRequestTypeException( msg );
        }
    }

    private void checkRequestParameter( Map<String, String[]> normalizedParameterMap ) {
        checkSingleRequiredParameter( normalizedParameterMap, REQUEST );
    }

    private void checkDescribeProcessParameters( Map<String, String[]> normalizedParameterMap ) {
        checkIdentifiersParameter( normalizedParameterMap );
    }

    private void checkExecuteParameters( Map<String, String[]> normalizedParameterMap ) {
        checkIdentifierParameter( normalizedParameterMap );
    }

    private void checkIdentifiersParameter( Map<String, String[]> normalizedParameterMap ) {
        checkRequiredParameter( normalizedParameterMap, IDENTIFIER );
    }

    private void checkIdentifierParameter( Map<String, String[]> normalizedParameterMap ) {
        checkSingleRequiredParameter( normalizedParameterMap, IDENTIFIER );
    }

    private List<String> extractIdentifier( String[] identifierParameters ) {
{'fraction_non_alphanumeric': 0.07766917854211744, 'fraction_numerical': 0.0016414235618891293, 'mean_word_length': 4.005227781926811, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6653409', 'n_tokens_mistral': 3241, 'n_tokens_neox': 3189, 'n_words': 823}