|
Apache CXF example source code file (JSONProvider.java)
The Apache CXF JSONProvider.java source code/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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.apache.cxf.jaxrs.provider; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.SequenceInputStream; import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.util.Collection; import java.util.Enumeration; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import javax.ws.rs.Consumes; import javax.ws.rs.Produces; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import javax.ws.rs.ext.Provider; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.namespace.QName; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; import org.apache.cxf.jaxrs.ext.MessageContext; import org.apache.cxf.jaxrs.utils.HttpUtils; import org.apache.cxf.jaxrs.utils.InjectionUtils; import org.apache.cxf.jaxrs.utils.schemas.SchemaHandler; import org.codehaus.jettison.mapped.Configuration; import org.codehaus.jettison.mapped.SimpleConverter; import org.codehaus.jettison.mapped.TypeConverter; @Produces("application/json") @Consumes("application/json") @Provider public class JSONProvider extends AbstractJAXBProvider { private static final String MAPPED_CONVENTION = "mapped"; private static final String BADGER_FISH_CONVENTION = "badgerfish"; private ConcurrentHashMap<String, String> namespaceMap = new ConcurrentHashMap<String, String>(); private boolean serializeAsArray; private List<String> arrayKeys; private boolean unwrapped; private String wrapperName; private Map<String, String> wrapperMap; private boolean dropRootElement; private boolean dropCollectionWrapperElement; private boolean ignoreMixedContent; private boolean writeXsiType = true; private boolean readXsiType = true; private boolean ignoreNamespaces; private String convention = MAPPED_CONVENTION; private TypeConverter typeConverter; public void setConvention(String value) { if (!MAPPED_CONVENTION.equals(value) && !BADGER_FISH_CONVENTION.equals(value)) { throw new IllegalArgumentException("Unsupported convention \"" + value); } convention = value; } public void setConvertTypesToStrings(boolean convert) { if (convert) { this.setTypeConverter(new SimpleConverter()); } } public void setTypeConverter(TypeConverter converter) { this.typeConverter = converter; } public void setIgnoreNamespaces(boolean ignoreNamespaces) { this.ignoreNamespaces = ignoreNamespaces; } @Context public void setMessageContext(MessageContext mc) { super.setContext(mc); } public void setDropRootElement(boolean drop) { this.dropRootElement = drop; } public void setDropCollectionWrapperElement(boolean drop) { this.dropCollectionWrapperElement = drop; } public void setIgnoreMixedContent(boolean ignore) { this.ignoreMixedContent = ignore; } public void setSupportUnwrapped(boolean unwrap) { this.unwrapped = unwrap; } public void setWrapperName(String wName) { wrapperName = wName; } public void setWrapperMap(Map<String, String> map) { wrapperMap = map; } public void setEnableBuffering(boolean enableBuf) { super.setEnableBuffering(enableBuf); } public void setConsumeMediaTypes(List<String> types) { super.setConsumeMediaTypes(types); } public void setProduceMediaTypes(List<String> types) { super.setProduceMediaTypes(types); } public void setSchemas(List<String> locations) { super.setSchemaLocations(locations); } public void setSchemaHandler(SchemaHandler handler) { super.setSchema(handler.getSchema()); } public void setSerializeAsArray(boolean asArray) { this.serializeAsArray = asArray; } public void setArrayKeys(List<String> keys) { this.arrayKeys = keys; } public void setNamespaceMap(Map<String, String> namespaceMap) { this.namespaceMap.putAll(namespaceMap); } public Object readFrom(Class<Object> type, Type genericType, Annotation[] anns, MediaType mt, MultivaluedMap<String, String> headers, InputStream is) throws IOException { try { Class<?> theType = getActualType(type, genericType, anns); Unmarshaller unmarshaller = createUnmarshaller(theType, genericType); InputStream realStream = getInputStream(type, genericType, is); XMLStreamReader xsw = createReader(type, realStream); Object response = null; if (JAXBElement.class.isAssignableFrom(type) || unmarshalAsJaxbElement || jaxbElementClassMap != null && jaxbElementClassMap.containsKey(theType.getName())) { response = unmarshaller.unmarshal(xsw, theType); } else { response = unmarshaller.unmarshal(xsw); } if (response instanceof JAXBElement && !JAXBElement.class.isAssignableFrom(type)) { response = ((JAXBElement)response).getValue(); } response = checkAdapter(response, type, anns, false); return response; } catch (JAXBException e) { handleJAXBException(e); } catch (XMLStreamException e) { throw new WebApplicationException(e); } catch (WebApplicationException e) { throw e; } catch (Exception e) { throw new WebApplicationException(e); } // unreachable return null; } protected XMLStreamReader createReader(Class<?> type, InputStream is) throws Exception { if (BADGER_FISH_CONVENTION.equals(convention)) { return JSONUtils.createBadgerFishReader(is); } else { XMLStreamReader reader = JSONUtils.createStreamReader(is, readXsiType, namespaceMap); return createTransformReaderIfNeeded(reader, is); } } protected InputStream getInputStream(Class<Object> cls, Type type, InputStream is) throws Exception { if (unwrapped) { String rootName = getRootName(cls, type); InputStream isBefore = new ByteArrayInputStream(rootName.getBytes()); String after = "}"; InputStream isAfter = new ByteArrayInputStream(after.getBytes()); final InputStream[] streams = new InputStream[]{isBefore, is, isAfter}; Enumeration<InputStream> list = new Enumeration Other Apache CXF examples (source code examples)Here is a short list of links related to this Apache CXF JSONProvider.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.