Search in sources :

Example 1 with DecoderKey

use of org.n52.svalbard.decode.DecoderKey in project arctic-sea by 52North.

the class AbstractXmlBinding method decode.

protected T decode(HttpServletRequest request) throws OwsExceptionReport {
    String characterEncoding = getCharacterEncoding(request);
    String xmlString = xmlToString(request, characterEncoding);
    LOGGER.debug("XML-REQUEST: {}", xmlString);
    DecoderKey key = getDecoderKey(xmlString, characterEncoding);
    LOGGER.trace("Found decoder key: {}", key);
    Decoder<T, String> decoder = getDecoder(key);
    if (decoder == null) {
        // if this a GetCapabilities request, then the service is not supported
        String opOrType = null;
        Optional<String> service = Optional.empty();
        if (key instanceof XmlNamespaceOperationDecoderKey) {
            XmlNamespaceOperationDecoderKey xmlNamespaceKey = (XmlNamespaceOperationDecoderKey) key;
            opOrType = xmlNamespaceKey.getType();
        } else if (key instanceof XmlStringOperationDecoderKey) {
            XmlStringOperationDecoderKey xmlStringKey = (XmlStringOperationDecoderKey) key;
            opOrType = xmlStringKey.getOperation();
            service = Optional.of(xmlStringKey.getService());
        }
        if (OWSConstants.Operations.GetCapabilities.toString().equalsIgnoreCase(opOrType)) {
            if (service.isPresent()) {
                throw new InvalidParameterValueException(OWSConstants.GetCapabilitiesParams.service, service.get()).withMessage("The service '%s' is not supported.", service);
            } else {
                throw new MissingParameterValueException(OWSConstants.GetCapabilitiesParams.service).withMessage("The parameter '%s' is missing.", OWSConstants.GetCapabilitiesParams.service);
            }
        } else {
            throw new InvalidParameterValueException().withMessage("No decoder found for incoming message " + "based on derived decoder key: %s\nMessage: %s", key, xmlString);
        }
    } else {
        LOGGER.trace("Using decoder: {}", decoder);
    }
    try {
        return decoder.decode(xmlString);
    } catch (OwsDecodingException ex) {
        throw ex.getCause();
    } catch (DecodingException ex) {
        throw new NoApplicableCodeException().withMessage(ex.getMessage()).causedBy(ex);
    }
}
Also used : XmlNamespaceOperationDecoderKey(org.n52.svalbard.decode.XmlNamespaceOperationDecoderKey) MissingParameterValueException(org.n52.shetland.ogc.ows.exception.MissingParameterValueException) OwsDecodingException(org.n52.iceland.coding.decode.OwsDecodingException) InvalidParameterValueException(org.n52.shetland.ogc.ows.exception.InvalidParameterValueException) NoApplicableCodeException(org.n52.shetland.ogc.ows.exception.NoApplicableCodeException) DecoderKey(org.n52.svalbard.decode.DecoderKey) XmlStringOperationDecoderKey(org.n52.svalbard.decode.XmlStringOperationDecoderKey) XmlNamespaceOperationDecoderKey(org.n52.svalbard.decode.XmlNamespaceOperationDecoderKey) DecodingException(org.n52.svalbard.decode.exception.DecodingException) OwsDecodingException(org.n52.iceland.coding.decode.OwsDecodingException) XmlStringOperationDecoderKey(org.n52.svalbard.decode.XmlStringOperationDecoderKey)

Example 2 with DecoderKey

use of org.n52.svalbard.decode.DecoderKey in project arctic-sea by 52North.

the class AbstractXmlBinding method getDecoderKey.

@VisibleForTesting
protected DecoderKey getDecoderKey(String xmlContent, String characterEncoding) throws CodedException {
    try (ByteArrayInputStream stream = new ByteArrayInputStream(xmlContent.getBytes(characterEncoding))) {
        // FIXME do not parse the complete request, if we only need the first element
        Document document = documentFactory.newDocumentBuilder().parse(stream);
        Element element = document.getDocumentElement();
        // TODO is this REALLY needed!?
        element.normalize();
        if (element.hasAttributes() && element.hasAttribute(OWSConstants.RequestParams.service.name())) {
            OwsOperationKey operationKey = getOperationKey(element);
            XmlStringOperationDecoderKey decoderKey = new XmlStringOperationDecoderKey(operationKey, getDefaultContentType());
            return decoderKey;
        } else {
            return getNamespaceOperationDecoderKey(element);
        }
    } catch (SAXException | IOException | ParserConfigurationException e) {
        throw new NoApplicableCodeException().causedBy(e).withMessage("An error occured when parsing the request! Message: %s", e.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) NoApplicableCodeException(org.n52.shetland.ogc.ows.exception.NoApplicableCodeException) Element(org.w3c.dom.Element) XmlStringOperationDecoderKey(org.n52.svalbard.decode.XmlStringOperationDecoderKey) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) OwsOperationKey(org.n52.shetland.ogc.ows.service.OwsOperationKey) SAXException(org.xml.sax.SAXException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with DecoderKey

use of org.n52.svalbard.decode.DecoderKey in project arctic-sea by 52North.

the class CodingHelper method xmlDecoderKeysForOperation.

public static Set<DecoderKey> xmlDecoderKeysForOperation(String service, String version, String... operations) {
    HashSet<DecoderKey> set = new HashSet<>(operations.length);
    for (String o : operations) {
        set.add(new OperationDecoderKey(service, version, o, MediaTypes.TEXT_XML));
        set.add(new OperationDecoderKey(service, version, o, MediaTypes.APPLICATION_XML));
    }
    return set;
}
Also used : DecoderKey(org.n52.svalbard.decode.DecoderKey) OperationDecoderKey(org.n52.svalbard.decode.OperationDecoderKey) XmlStringOperationDecoderKey(org.n52.svalbard.decode.XmlStringOperationDecoderKey) XmlNamespaceDecoderKey(org.n52.svalbard.decode.XmlNamespaceDecoderKey) OperationDecoderKey(org.n52.svalbard.decode.OperationDecoderKey) XmlStringOperationDecoderKey(org.n52.svalbard.decode.XmlStringOperationDecoderKey) HashSet(java.util.HashSet)

Example 4 with DecoderKey

use of org.n52.svalbard.decode.DecoderKey in project arctic-sea by 52North.

the class CodingHelper method xmlDecoderKeysForOperation.

public static Set<DecoderKey> xmlDecoderKeysForOperation(String service, String version, Enum<?>... operations) {
    HashSet<DecoderKey> set = new HashSet<>(operations.length);
    for (Enum<?> o : operations) {
        set.add(new OperationDecoderKey(service, version, o.name(), MediaTypes.TEXT_XML));
        set.add(new OperationDecoderKey(service, version, o.name(), MediaTypes.APPLICATION_XML));
    }
    return set;
}
Also used : DecoderKey(org.n52.svalbard.decode.DecoderKey) OperationDecoderKey(org.n52.svalbard.decode.OperationDecoderKey) XmlStringOperationDecoderKey(org.n52.svalbard.decode.XmlStringOperationDecoderKey) XmlNamespaceDecoderKey(org.n52.svalbard.decode.XmlNamespaceDecoderKey) OperationDecoderKey(org.n52.svalbard.decode.OperationDecoderKey) XmlStringOperationDecoderKey(org.n52.svalbard.decode.XmlStringOperationDecoderKey) HashSet(java.util.HashSet)

Example 5 with DecoderKey

use of org.n52.svalbard.decode.DecoderKey in project arctic-sea by 52North.

the class AbstractStringRequestDecoder method decode.

@Override
public OwsServiceCommunicationObject decode(String string) throws DecodingException {
    XmlObject xml = CodingHelper.readXML(string);
    DecoderKey key = CodingHelper.getDecoderKey(xml);
    Decoder<OwsServiceCommunicationObject, XmlObject> decoder = decoderRepository.getDecoder(key);
    if (decoder == null) {
        throw new NoDecoderForKeyException(key);
    }
    return decoder.decode(xml);
}
Also used : OwsServiceCommunicationObject(org.n52.shetland.ogc.ows.service.OwsServiceCommunicationObject) NoDecoderForKeyException(org.n52.svalbard.decode.exception.NoDecoderForKeyException) XmlObject(org.apache.xmlbeans.XmlObject)

Aggregations

DecoderKey (org.n52.svalbard.decode.DecoderKey)9 OperationDecoderKey (org.n52.svalbard.decode.OperationDecoderKey)8 XmlStringOperationDecoderKey (org.n52.svalbard.decode.XmlStringOperationDecoderKey)6 Test (org.junit.Test)5 XmlNamespaceOperationDecoderKey (org.n52.svalbard.decode.XmlNamespaceOperationDecoderKey)5 HashSet (java.util.HashSet)4 XmlNamespaceDecoderKey (org.n52.svalbard.decode.XmlNamespaceDecoderKey)4 XmlObject (org.apache.xmlbeans.XmlObject)2 NoApplicableCodeException (org.n52.shetland.ogc.ows.exception.NoApplicableCodeException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 OwsDecodingException (org.n52.iceland.coding.decode.OwsDecodingException)1 FeatureCollection (org.n52.shetland.ogc.om.features.FeatureCollection)1 InvalidParameterValueException (org.n52.shetland.ogc.ows.exception.InvalidParameterValueException)1 MissingParameterValueException (org.n52.shetland.ogc.ows.exception.MissingParameterValueException)1 OwsOperationKey (org.n52.shetland.ogc.ows.service.OwsOperationKey)1 OwsServiceCommunicationObject (org.n52.shetland.ogc.ows.service.OwsServiceCommunicationObject)1 GetFeatureOfInterestResponse (org.n52.shetland.ogc.sos.response.GetFeatureOfInterestResponse)1