Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.wink.providers.jettison;
 
 
 
A Jettison JAXB provider. By default, use the MappedNamespace convention. Namespace mapping needs to be set if namespaces are used. In Application sub-class, use javax.ws.rs.core.Application.getSingletons() to add to application.
 
 public class JettisonJAXBProvider extends AbstractJAXBProvider implements
 
     private static final Logger logger = LoggerFactory.getLogger(JettisonJAXBProvider.class);
 
     final private boolean       isBadgerFishConventionUsed;
 
     final private Configuration inputConfiguration;
 
     final private Configuration outputConfiguration;
 
     private boolean             isReadable;
 
     private boolean             isWritable;
 
     public JettisonJAXBProvider() {
         this(truenullnull);
     }
 
     public JettisonJAXBProvider(boolean isBadgerFishConventionUsed,
                                 Configuration reader,
                                 Configuration writer) {
         this. = isBadgerFishConventionUsed;
         if (reader != null) {
             this. = reader;
         } else {
             this. = new Configuration(new HashMap<StringString>());
         }
         if (writer != null) {
             this. = writer;
         } else {
            this. = new Configuration(new HashMap<StringString>());
        }
        // see http://jira.codehaus.org/browse/JETTISON-74 . reading disabled
        // for now
         = false;
         = true;
    }
    public void setUseAsReader(boolean isReadable) {
        this. = isReadable;
    }
    public void setUseAsWriter(boolean isWritable) {
        this. = isWritable;
    }
    public boolean isReadable(Class<?> type,
                              Type genericType,
                              Annotation[] annotations,
                              MediaType mediaType) {
        return  && isJAXBObject(typegenericType);
    }
    public Object readFrom(Class<Objecttype,
                           Type genericType,
                           Annotation[] annotations,
                           MediaType mediaType,
                           MultivaluedMap<StringStringhttpHeaders,
                           InputStream entityStreamthrows IOExceptionWebApplicationException {
        Unmarshaller unmarshaller = null;
        Object unmarshaledResource = null;
        try {
            JAXBContext context = getContext(typemediaType);
            unmarshaller = getJAXBUnmarshaller(typecontextmediaType);
            XMLStreamReader xsr = null;
            if () {
                xsr = new BadgerFishXMLInputFactory().createXMLStreamReader(entityStream);
            } else {
                xsr =
                    new MappedXMLInputFactory()
                        .createXMLStreamReader(entityStream);
            }
            if (type.isAnnotationPresent(XmlRootElement.class)) {
                unmarshaledResource = unmarshaller.unmarshal(xsr);
                if (unmarshaledResource instanceof JAXBElement) {
                    // this can happen if the JAXBContext object used to create
                    // the unmarshaller
                    // was created using the package name string instead of a
                    // class object and the
                    // ObjectFactory has a creator method for the desired object
                    // that returns
                    // JAXBElement. But we know better; the 'type' param passed
                    // in here had the
                    // XmlRootElement on it, so we know the desired return
                    // object type is NOT
                    // JAXBElement, thus:
                    unmarshaledResource = ((JAXBElement)unmarshaledResource).getValue();
                }
            } else {
                unmarshaledResource = unmarshaller.unmarshal(xsrtype).getValue();
            }
        } catch (JAXBException e) {
            if (.isErrorEnabled()) {
                .error(Messages.getMessage("jaxbFailToUnmarshal"type.getName()), e); //$NON-NLS-1$
            }
            throw new WebApplicationException(e..);
        } catch (XMLStreamException e) {
            if (.isErrorEnabled()) {
                .error(Messages.getMessage("jaxbFailToUnmarshal"type.getName()), e); //$NON-NLS-1$
            }
            throw new WebApplicationException(e..);
        }
        return unmarshaledResource;
    }
    public long getSize(Object t,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType) {
        return -1;
    }
    public boolean isWriteable(Class<?> type,
                               Type genericType,
                               Annotation[] annotations,
                               MediaType mediaType) {
        return  && isJAXBObject(typegenericType);
    }
    public void writeTo(Object t,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType,
                        MultivaluedMap<StringObjecthttpHeaders,
                        OutputStream entityStreamthrows IOExceptionWebApplicationException {
        try {
            JAXBContext context = getContext(typemediaType);
            Marshaller marshaller = getJAXBMarshaller(typecontextmediaType);
            Object entityToMarshal = getEntityToMarshal(ttype);
            // Use an OutputStream directly instead of a Writer for performance.
            XMLStreamWriter xsw = null;
            if () {
                xsw = new BadgerFishXMLStreamWriter(new OutputStreamWriter(entityStream));
            } else {
                MappedNamespaceConvention con = new MappedNamespaceConvention();
                xsw = new MappedXMLStreamWriter(connew OutputStreamWriter(entityStream));
            }
            marshaller.marshal(entityToMarshalxsw);
        } catch (JAXBException e) {
            if (.isErrorEnabled()) {
                .error(Messages.getMessage("jaxbFailToMarshal"type.getName()), e); //$NON-NLS-1$
            }
            throw new WebApplicationException(e);
        }
    }
New to GrepCode? Check out our FAQ X