<commit_before>//$HeadURL$
/*----------------------------------------------------------------------------
 This file is part of deegree, http://deegree.org/
 Copyright (C) 2001-2009 by:
 Department of Geography, University of Bonn
 and
 lat/lon GmbH

 This library is free software; you can redistribute it and/or modify it under
 the terms of the GNU Lesser General Public License as published by the Free
 Software Foundation; either version 2.1 of the License, or (at your option)
 any later version.
 This library is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 details.
 You should have received a copy of the GNU Lesser General Public License
 along with this library; if not, write to the Free Software Foundation, Inc.,
 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

 Contact information:

 lat/lon GmbH
 Aennchenstr. 19, 53177 Bonn
 Germany
 http://lat-lon.de/

 Department of Geography, University of Bonn
 Prof. Dr. <NAME>
 Postfach 1147, 53001 Bonn
 Germany
 http://www.geographie.uni-bonn.de/deegree/

 e-mail: <EMAIL>
 ----------------------------------------------------------------------------*/

package org.deegree.services.wps;

import static org.deegree.protocol.wps.WPSConstants.VERSION_100;
import static org.deegree.protocol.wps.WPSConstants.WPS_100_NS;
import static org.deegree.services.controller.ows.OWSException.OPERATION_NOT_SUPPORTED;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.commons.fileupload.FileItem;
import org.deegree.commons.tom.ows.CodeType;
import org.deegree.commons.tom.ows.Version;
import org.deegree.commons.utils.FileUtils;
import org.deegree.commons.utils.Pair;
import org.deegree.commons.utils.TempFileManager;
import org.deegree.commons.utils.kvp.KVPUtils;
import org.deegree.commons.utils.kvp.MissingParameterException;
import org.deegree.commons.xml.NamespaceContext;
import org.deegree.commons.xml.XMLAdapter;
import org.deegree.commons.xml.XPath;
import org.deegree.cs.exceptions.UnknownCRSException;
import org.deegree.process.jaxb.java.ProcessDefinition;
import org.deegree.protocol.ows.capabilities.GetCapabilities;
import org.deegree.protocol.ows.capabilities.GetCapabilitiesKVPParser;
import org.deegree.protocol.wps.WPSConstants.WPSRequestType;
import org.deegree.protocol.wps.capabilities.GetCapabilitiesXMLAdapter;
import org.deegree.protocol.wps.describeprocess.DescribeProcessRequest;
import org.deegree.protocol.wps.describeprocess.DescribeProcessRequestKVPAdapter;
import org.deegree.protocol.wps.describeprocess.DescribeProcessRequestXMLAdapter;
import org.deegree.services.controller.AbstractOGCServiceController;
import org.deegree.services.controller.ImplementationMetadata;
import org.deegree.services.controller.OGCFrontController;
import org.deegree.services.controller.exception.ControllerException;
import org.deegree.services.controller.exception.ControllerInitException;
import org.deegree.services.controller.exception.serializer.XMLExceptionSerializer;
import org.deegree.services.controller.ows.OWSException;
import org.deegree.services.controller.ows.OWSException110XMLAdapter;
import org.deegree.services.controller.utils.HttpResponseBuffer;
import org.deegree.services.exception.ServiceInitException;
import org.deegree.services.jaxb.controller.DeegreeServiceControllerType;
import org.deegree.services.jaxb.metadata.DeegreeServicesMetadataType;
import org.deegree.services.jaxb.wps.DeegreeWPS;
import org.deegree.services.wps.capabilities.CapabilitiesXMLWriter;
import org.deegree.services.wps.describeprocess.DescribeProcessResponseXMLAdapter;
import org.deegree.services.wps.execute.ExecuteRequest;
import org.deegree.services.wps.execute.ExecuteRequestKVPAdapter;
import org.deegree.services.wps.execute.ExecuteRequestXMLAdapter;
import org.deegree.services.wps.execute.RawDataOutput;
import org.deegree.services.wps.execute.ResponseDocument;
import org.deegree.services.wps.execute.ResponseForm;
import org.deegree.services.wps.storage.OutputStorage;
import org.deegree.services.wps.storage.ResponseDocumentStorage;
import org.deegree.services.wps.storage.StorageManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Implementation of the <a href="http://www.opengeospatial.org/standards/wps">OpenGIS Web Processing Service</a> server
 * protocol.
 * <p>
 * Supported WPS protocol versions:
 * <ul>
 * <li>1.0.0</li>
 * </ul>
 * </p>
 * 
 * @see OGCFrontController
 * @see ProcessManager
 * @see ExecutionManager
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME> </a>
 * @author last edited by: $Author$
 * 
 * @version $Revision$, $Date$
 */
public class WPService extends AbstractOGCServiceController {

    private static final Logger LOG = LoggerFactory.getLogger( WPService.class );

    private static final String CONFIG_JAXB_PACKAGE = "org.deegree.services.jaxb.wps";

    private static final String CONFIG_SCHEMA = "/META-INF/schemas/wps/3.0.0/wps_configuration.xsd";

    private static final ImplementationMetadata<WPSRequestType> IMPLEMENTATION_METADATA = new ImplementationMetadata<WPSRequestType>() {
        {
            supportedVersions = new Version[] { VERSION_100 };
            handledNamespaces = new String[] { WPS_100_NS };
            handledRequests = WPSRequestType.class;
            supportedConfigVersions = new Version[] { Version.parseVersion( "3.0.0" ) };
        }
    };

    private static final CodeType ALL_PROCESSES_IDENTIFIER = new CodeType( "ALL" );

    private StorageManager storageManager;

    private ProcessManager service;

    private DeegreeWPS sc;

    private ExecutionManager executeHandler;

    private File serviceWSDLFile;

    private Map<CodeType, File> processIdToWSDL = new HashMap<CodeType, File>();

    @Override
    public void init( XMLAdapter controllerConf, DeegreeServicesMetadataType serviceMetadata,
                      DeegreeServiceControllerType mainConf )
                            throws ControllerInitException {

        LOG.info( "Initializing WPS." );
        init( serviceMetadata, mainConf, IMPLEMENTATION_METADATA, controllerConf );

        storageManager = new StorageManager( TempFileManager.getBaseDir() );

        NamespaceContext nsContext = new NamespaceContext();
        nsContext.addNamespace( "wps", "http://www.deegree.org/services/wps" );

        OMElement configEl = controllerConf.getRequiredElement( controllerConf.getRootElement(),
                                                                new XPath( "wps:ServiceConfiguration", nsContext ) );
        sc = (DeegreeWPS) unmarshallConfig( CONFIG_JAXB_PACKAGE, CONFIG_SCHEMA, configEl );

        URL controllerConfURL;
        try {
            controllerConfURL = new URL( controllerConf.getSystemId() );
            File resolvedProcessesDir = FileUtils.getAsFile( new URL( controllerConfURL, "../processes/" ) );
            this.service = new ProcessManager( resolvedProcessesDir );

            validateAndSetOfferedVersions( sc.getSupportedVersions().getVersion() );

            executeHandler = new ExecutionManager( this, storageManager );

            // WSDL stuff
            serviceWSDLFile = FileUtils.getAsFile( new URL( controllerConfURL, "service.wsdl" ) );
        } catch ( MalformedURLException e ) {
            throw new ControllerInitException( "Problem resolving file resource: " + e.getMessage() );
        } catch ( ServiceInitException e ) {
            throw new ControllerInitException( "Problem initializing service: " + e.getMessage() );
        }
    }

    @Override
    public void destroy() {
        service.destroy();
    }

    @Override
    public void doKVP( Map<String, String> kvpParamsUC, HttpServletRequest request, HttpResponseBuffer response,
                       List<FileItem> multiParts )
                            throws ServletException, IOException {
<commit_msg>Fixed startup showstopper bug.<commit_after>//$HeadURL$
/*----------------------------------------------------------------------------
 This file is part of deegree, http://deegree.org/
 Copyright (C) 2001-2009 by:
 Department of Geography, University of Bonn
 and
 lat/lon GmbH

 This library is free software; you can redistribute it and/or modify it under
 the terms of the GNU Lesser General Public License as published by the Free
 Software Foundation; either version 2.1 of the License, or (at your option)
 any later version.
 This library is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 details.
 You should have received a copy of the GNU Lesser General Public License
 along with this library; if not, write to the Free Software Foundation, Inc.,
 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

 Contact information:

 lat/lon GmbH
 Aennchenstr. 19, 53177 Bonn
 Germany
 http://lat-lon.de/

 Department of Geography, University of Bonn
 Prof. Dr. <NAME>
 Postfach 1147, 53001 Bonn
 Germany
 http://www.geographie.uni-bonn.de/deegree/

 e-mail: <EMAIL>
 ----------------------------------------------------------------------------*/

package org.deegree.services.wps;

import static org.deegree.protocol.wps.WPSConstants.VERSION_100;
import static org.deegree.protocol.wps.WPSConstants.WPS_100_NS;
import static org.deegree.services.controller.ows.OWSException.OPERATION_NOT_SUPPORTED;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.commons.fileupload.FileItem;
import org.deegree.commons.tom.ows.CodeType;
import org.deegree.commons.tom.ows.Version;
import org.deegree.commons.utils.FileUtils;
import org.deegree.commons.utils.Pair;
import org.deegree.commons.utils.TempFileManager;
import org.deegree.commons.utils.kvp.KVPUtils;
import org.deegree.commons.utils.kvp.MissingParameterException;
import org.deegree.commons.xml.NamespaceContext;
import org.deegree.commons.xml.XMLAdapter;
import org.deegree.commons.xml.XPath;
import org.deegree.cs.exceptions.UnknownCRSException;
import org.deegree.process.jaxb.java.ProcessDefinition;
import org.deegree.protocol.ows.capabilities.GetCapabilities;
import org.deegree.protocol.ows.capabilities.GetCapabilitiesKVPParser;
import org.deegree.protocol.wps.WPSConstants.WPSRequestType;
import org.deegree.protocol.wps.capabilities.GetCapabilitiesXMLAdapter;
import org.deegree.protocol.wps.describeprocess.DescribeProcessRequest;
import org.deegree.protocol.wps.describeprocess.DescribeProcessRequestKVPAdapter;
import org.deegree.protocol.wps.describeprocess.DescribeProcessRequestXMLAdapter;
import org.deegree.services.controller.AbstractOGCServiceController;
import org.deegree.services.controller.ImplementationMetadata;
import org.deegree.services.controller.OGCFrontController;
import org.deegree.services.controller.exception.ControllerException;
import org.deegree.services.controller.exception.ControllerInitException;
import org.deegree.services.controller.exception.serializer.XMLExceptionSerializer;
import org.deegree.services.controller.ows.OWSException;
import org.deegree.services.controller.ows.OWSException110XMLAdapter;
import org.deegree.services.controller.utils.HttpResponseBuffer;
import org.deegree.services.exception.ServiceInitException;
import org.deegree.services.jaxb.controller.DeegreeServiceControllerType;
import org.deegree.services.jaxb.metadata.DeegreeServicesMetadataType;
import org.deegree.services.jaxb.wps.DeegreeWPS;
import org.deegree.services.wps.capabilities.CapabilitiesXMLWriter;
import org.deegree.services.wps.describeprocess.DescribeProcessResponseXMLAdapter;
import org.deegree.services.wps.execute.ExecuteRequest;
import org.deegree.services.wps.execute.ExecuteRequestKVPAdapter;
import org.deegree.services.wps.execute.ExecuteRequestXMLAdapter;
import org.deegree.services.wps.execute.RawDataOutput;
import org.deegree.services.wps.execute.ResponseDocument;
import org.deegree.services.wps.execute.ResponseForm;
import org.deegree.services.wps.storage.OutputStorage;
import org.deegree.services.wps.storage.ResponseDocumentStorage;
import org.deegree.services.wps.storage.StorageManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Implementation of the <a href="http://www.opengeospatial.org/standards/wps">OpenGIS Web Processing Service</a> server
 * protocol.
 * <p>
 * Supported WPS protocol versions:
 * <ul>
 * <li>1.0.0</li>
 * </ul>
 * </p>
 * 
 * @see OGCFrontController
 * @see ProcessManager
 * @see ExecutionManager
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME> </a>
 * @author last edited by: $Author$
 * 
 * @version $Revision$, $Date$
 */
public class WPService extends AbstractOGCServiceController {

    private static final Logger LOG = LoggerFactory.getLogger( WPService.class );

    private static final String CONFIG_JAXB_PACKAGE = "org.deegree.services.jaxb.wps";

    private static final String CONFIG_SCHEMA = "/META-INF/schemas/wps/3.0.0/wps_configuration.xsd";

    private static final ImplementationMetadata<WPSRequestType> IMPLEMENTATION_METADATA = new ImplementationMetadata<WPSRequestType>() {
        {
            supportedVersions = new Version[] { VERSION_100 };
            handledNamespaces = new String[] { WPS_100_NS };
            handledRequests = WPSRequestType.class;
            supportedConfigVersions = new Version[] { Version.parseVersion( "3.0.0" ) };
        }
    };

    private static final CodeType ALL_PROCESSES_IDENTIFIER = new CodeType( "ALL" );

    private StorageManager storageManager;

    private ProcessManager service;

    private ExecutionManager executeHandler;

    private File serviceWSDLFile;

    private Map<CodeType, File> processIdToWSDL = new HashMap<CodeType, File>();

    @Override
    public void init( XMLAdapter controllerConf, DeegreeServicesMetadataType serviceMetadata,
                      DeegreeServiceControllerType mainConf )
                            throws ControllerInitException {

        LOG.info( "Initializing WPS." );
        init( serviceMetadata, mainConf, IMPLEMENTATION_METADATA, controllerConf );

        storageManager = new StorageManager( TempFileManager.getBaseDir() );

        DeegreeWPS sc = (DeegreeWPS) unmarshallConfig( CONFIG_JAXB_PACKAGE, CONFIG_SCHEMA, controllerConf.getRootElement() );

        URL controllerConfURL;
        try {
            controllerConfURL = new URL( controllerConf.getSystemId() );
            File resolvedProcessesDir = FileUtils.getAsFile( new URL( controllerConfURL, "../processes/" ) );
            this.service = new ProcessManager( resolvedProcessesDir );

            validateAndSetOfferedVersions( sc.getSupportedVersions().getVersion() );

            executeHandler = new ExecutionManager( this, storageManager );

            // WSDL stuff
            serviceWSDLFile = FileUtils.getAsFile( new URL( controllerConfURL, "service.wsdl" ) );
        } catch ( MalformedURLException e ) {
            throw new ControllerInitException( "Problem resolving file resource: " + e.getMessage() );
        } catch ( ServiceInitException e ) {
            throw new ControllerInitException( "Problem initializing service: " + e.getMessage() );
        }
    }

    @Override
    public void destroy() {
        service.destroy();
    }

    @Override
    public void doKVP( Map<String, String> kvpParamsUC, HttpServletRequest request, HttpResponseBuffer response,
                       List<FileItem> multiParts )
                            throws ServletException, IOException {

{'fraction_non_alphanumeric': 0.11008314790959128, 'fraction_numerical': 0.007729242300035133, 'mean_word_length': 3.96049956433343, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 5, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4442603', 'n_tokens_mistral': 5016, 'n_tokens_neox': 4847, 'n_words': 1078}