/*
* Constellation - An open source and standard compliant SDI
* http://www.constellation-sdi.org
*
* Copyright 2014 Geomatys.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.constellation.ws.rs.provider;
import org.apache.sis.util.logging.Logging;
import org.constellation.configuration.LayerContext;
import org.constellation.configuration.LayerList;
import org.constellation.generic.database.GenericDatabaseMarshallerPool;
import org.glassfish.jersey.jettison.JettisonConfig;
import org.glassfish.jersey.jettison.JettisonJaxbContext;
import org.glassfish.jersey.jettison.JettisonMarshaller;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Write a layer context response in the stream.
*
* @author Guilhem Legal (Geomatys)
* @author Benjamin Garcia (Geomatys)
*
* @version 0.9
*
* @see Provider
* @see Produces
*/
@Provider
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public final class LayerContextWriter implements MessageBodyWriter {
private static final Logger LOGGER = Logging.getLogger("org.constellation.ws.rs.provider");
@Override
public boolean isWriteable(Class type, Type type1, Annotation[] antns, MediaType mt) {
return LayerContext.class.isAssignableFrom(type) || LayerList.class.isAssignableFrom(type);
}
@Override
public long getSize(Object t, Class type, Type type1, Annotation[] antns, MediaType mt) {
return -1;
}
@Override
public void writeTo(Object r, Class type, Type type1, Annotation[] antns, MediaType mt,
MultivaluedMap mm, OutputStream out) throws IOException, WebApplicationException {
try {
// if it's a json POST, create a JSonMarshaller.
if (mt.equals(MediaType.APPLICATION_JSON_TYPE)) {
//transform xlm namespace to json namespace
Map<String, String> nSMap = new HashMap<>(0);
nSMap.put("http://www.constellation.org/config", "constellation-config");
// create json marshaller configuration and context
JettisonConfig config = JettisonConfig.mappedJettison().xml2JsonNs(nSMap).build();
JettisonJaxbContext cxtx = new JettisonJaxbContext(config, "org.constellation.configuration:" +
"org.constellation.generic.database:" +
"org.geotoolkit.ogc.xml.v110:" +
"org.apache.sis.internal.jaxb.geometry:" +
"org.geotoolkit.gml.xml.v311");
// create marshaller
JettisonMarshaller jsonMarshaller = cxtx.createJsonMarshaller();
// Marshall object
jsonMarshaller.marshallToJSON(r, out);
} else {
// Default : use xml marshaller
final Marshaller m = GenericDatabaseMarshallerPool.getInstance().acquireMarshaller();
m.marshal(r, out);
GenericDatabaseMarshallerPool.getInstance().recycle(m);
}
} catch (JAXBException ex) {
LOGGER.log(Level.SEVERE, "JAXB exception while writing the layerContext", ex);
}
}
}