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

org.n52.sos.ds.AbstractResultHandlingHandler Maven / Gradle / Ivy

/*
 * Copyright (C) 2012-2019 52°North Initiative for Geospatial Open Source
 * Software GmbH
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation.
 *
 * If the program is linked with libraries which are licensed under one of
 * the following licenses, the combination of the program with the linked
 * library is not considered a "derivative work" of the program:
 *
 *     - Apache License, version 2.0
 *     - Apache Software License, version 1.0
 *     - GNU Lesser General Public License, version 3
 *     - Mozilla Public License, versions 1.0, 1.1 and 2.0
 *     - Common Development and Distribution License (CDDL), version 1.0
 *
 * Therefore the distribution of the program linked with libraries licensed
 * under the aforementioned licenses, is permitted by the copyright holders
 * if the distribution is compliant with both the GNU General Public
 * License version 2 and the aforementioned licenses.
 *
 * This program 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 General
 * Public License for more details.
 */
package org.n52.sos.ds;

import javax.inject.Inject;

import org.apache.xmlbeans.XmlObject;
import org.n52.shetland.ogc.ows.exception.CodedException;
import org.n52.shetland.ogc.ows.exception.NoApplicableCodeException;
import org.n52.shetland.ogc.ows.exception.OwsExceptionReport;
import org.n52.shetland.ogc.sos.SosResultEncoding;
import org.n52.shetland.ogc.sos.SosResultStructure;
import org.n52.shetland.ogc.swe.SweAbstractDataComponent;
import org.n52.shetland.ogc.swe.SweDataArray;
import org.n52.shetland.ogc.swe.SweDataRecord;
import org.n52.shetland.ogc.swe.encoding.SweAbstractEncoding;
import org.n52.svalbard.decode.Decoder;
import org.n52.svalbard.decode.DecoderKey;
import org.n52.svalbard.decode.DecoderRepository;
import org.n52.svalbard.decode.exception.DecodingException;
import org.n52.svalbard.util.CodingHelper;
import org.n52.svalbard.util.XmlHelper;

/**
 * Abstract class for Result Handling operation Handlers to provide common
 * methods
 *
 * Renamed, in version 4.x called AbstractResultHandlingDAO
 *
 * @author Carsten Hollmann
 * @since 5.0.0
 *
 */
public abstract class AbstractResultHandlingHandler extends AbstractSosOperationHandler {

    private DecoderRepository decodingRepository;

    public AbstractResultHandlingHandler(String service, String operationName) {
        super(service, operationName);
    }

    @Inject
    public void setDecoderRepository(DecoderRepository decodingRepository) {
        this.decodingRepository = decodingRepository;
    }

    /**
     * Get internal SweDataRecord from internal ResultStructure
     *
     * @param resultStructure
     *            Internal ResultStructure
     * @return internal SweDataRecord
     * @throws OwsExceptionReport
     *             If the ResultStructure is not supported
     */
    protected SweDataRecord setRecordFrom(final SweAbstractDataComponent resultStructure) throws OwsExceptionReport {
        SweDataRecord record = null;
        if (resultStructure instanceof SweDataArray
                && ((SweDataArray) resultStructure).getElementType() instanceof SweDataRecord) {
            final SweDataArray array = (SweDataArray) resultStructure;
            record = (SweDataRecord) array.getElementType();
        } else if (resultStructure instanceof SweDataRecord) {
            record = (SweDataRecord) resultStructure;
        } else {
            throw new NoApplicableCodeException().withMessage("Unsupported ResultStructure!");
        }
        return record;
    }

    protected SosResultStructure createSosResultStructure(String resultStructure) throws CodedException {
        SweAbstractDataComponent abstractDataComponent = (SweAbstractDataComponent) decode(resultStructure);
        if (abstractDataComponent != null) {
            return new SosResultStructure(abstractDataComponent, resultStructure);
        }
        return new SosResultStructure(resultStructure);
    }

    protected SosResultEncoding createSosResultEncoding(String resultEncoding) throws CodedException {
        SweAbstractEncoding abstractEncoding = (SweAbstractEncoding) decode(resultEncoding);
        if (abstractEncoding != null) {
            return new SosResultEncoding(abstractEncoding, resultEncoding);
        }
        return new SosResultEncoding(resultEncoding);
    }

    protected Object decode(String xml) throws CodedException {
        try {
            XmlObject xmlObject = XmlHelper.parseXmlString(xml);
            DecoderKey decoderKey = CodingHelper.getDecoderKey(xmlObject);
            Decoder decoder = decodingRepository.getDecoder(decoderKey);
            if (decoder != null) {
                return decoder.decode(xmlObject);
            } else {
                throw new NoApplicableCodeException().withMessage("No decoder found for %s",
                        xmlObject.getClass().getName());
            }
        } catch (DecodingException de) {
            throw new NoApplicableCodeException().causedBy(de);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy