All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opengis.cite.wfs20.nsg.utils.NsgWfsClient Maven / Gradle / Ivy

package org.opengis.cite.wfs20.nsg.utils;

import static javax.xml.xpath.XPathConstants.STRING;
import static org.opengis.cite.iso19142.ProtocolBinding.GET;
import static org.opengis.cite.wfs20.nsg.utils.NamespaceUtils.withStandardBindings;

import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ws.rs.core.UriBuilder;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.xpath.XPathExpressionException;

import org.opengis.cite.iso19142.ProtocolBinding;
import org.opengis.cite.iso19142.util.WFSClient;
import org.opengis.cite.iso19142.util.XMLUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

/**
 * Enhanced the {@link WFSClient} by the support of transforming PageResults POST binding requests to GET query string
 *
 * @author Lyn Goltz 
 */
public class NsgWfsClient extends WFSClient {

    private static final Logger LOGR = Logger.getLogger( NsgWfsClient.class.getPackage().getName() );

    NsgWfsClient() {
    }

    public NsgWfsClient( Document wfsMetadata ) {
        super( wfsMetadata );
    }

    @Override
    public ClientResponse submitRequest( Source entity, ProtocolBinding binding, URI endpoint ) {
        if ( entity instanceof DOMSource ) {
            Node node = ( (DOMSource) entity ).getNode();
            String docElemName = node.getLocalName();
            if ( "PageResults".equals( docElemName ) && GET.equals( binding ) ) {
                String queryString = createQueryString( node );
                WebResource resource = client.resource( endpoint );
                URI requestURI = UriBuilder.fromUri( resource.getURI() ).replaceQuery( queryString ).build();
                LOGR.log( Level.FINE, String.format( "Request URI: %s", requestURI ) );
                resource = resource.uri( requestURI );
                return resource.get( ClientResponse.class );
            }
        }
        return super.submitRequest( entity, binding, endpoint );
    }

    /**
     * Creates a query string from the passed PageResults request
     * 
     * @param entity
     *            never null
     * @return the query string, never null
     */
    String createQueryString( Node entity ) {
        try {
            StringBuilder sb = new StringBuilder();
            appendValue( sb, entity, "service", "WFS" );
            appendValue( sb, entity, "version", "2.0.2" );
            sb.append( "REQUEST=PageResults&" );
            appendValue( sb, entity, "startIndex", null );
            appendValue( sb, entity, "count", null );
            appendValue( sb, entity, "outputFormat", null );
            appendValue( sb, entity, "resultType", null );
            appendValue( sb, entity, "timeout", null );
            appendResultSetId( sb, entity );
            return sb.toString();
        } catch ( XPathExpressionException e ) {
            throw new RuntimeException( "Could nor create query string for PageResults request with GET" );
        }
    }

    private void appendResultSetId( StringBuilder sb, Node entity )
                            throws XPathExpressionException {
        String xpath = "//nsg:PageResults/nsg:resultSetID";
        String resultSetId = (String) XMLUtils.evaluateXPath( entity, xpath, withStandardBindings().getAllBindings(),
                                                              STRING );
        sb.append( "RESULTSETID=" ).append( resultSetId );
    }

    private void appendValue( StringBuilder sb, Node attributes, String parameter, String defaultValue )
                            throws XPathExpressionException {
        String value = parseValue( attributes, parameter, defaultValue );
        if ( value != null )
            sb.append( parameter.toUpperCase() ).append( "=" ).append( value ).append( "&" );
    }

    private String parseValue( Node entity, String parameter, String defaultValue )
                            throws XPathExpressionException {
        String xpath = "//nsg:PageResults/@" + parameter;
        String value = (String) XMLUtils.evaluateXPath( entity, xpath, withStandardBindings().getAllBindings(), STRING );
        if ( value != null && !value.isEmpty() )
            return value;
        return defaultValue;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy