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 JAXBElement provider. By default, use the BadgerFishConvention. 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 JettisonJAXBElementProvider extends AbstractJAXBProvider implements
 
     private static final Logger logger = LoggerFactory.getLogger(JettisonJAXBElementProvider.class);
 
     final private boolean       isBadgerFishConventionUsed;
 
     final private Configuration inputConfiguration;
 
     final private Configuration outputConfiguration;
 
     private boolean             isReadable;
 
     private boolean             isWritable;
 
     public JettisonJAXBElementProvider() {
         this(truenullnull);
     }
 
     public JettisonJAXBElementProvider(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  && isJAXBElement(typegenericType);
    }
    public JAXBElement<?> readFrom(Class<JAXBElement<?>> type,
                                   Type genericType,
                                   Annotation[] annotations,
                                   MediaType mediaType,
                                   MultivaluedMap<StringStringhttpHeaders,
                                   InputStream entityStreamthrows IOException,
        WebApplicationException {
        ParameterizedType parameterizedType = (ParameterizedType)genericType;
        Class<?> classToFill = (Class<?>)parameterizedType.getActualTypeArguments()[0];
        JAXBElement<?> unmarshaledResource = null;
        Unmarshaller unmarshaller = null;
        try {
            JAXBContext context = getContext(classToFillmediaType);
            unmarshaller = getJAXBUnmarshaller(typecontextmediaType);
            XMLStreamReader xsr = null;
            if () {
                xsr = new BadgerFishXMLInputFactory().createXMLStreamReader(entityStream);
            } else {
                xsr =
                    new MappedXMLInputFactory()
                        .createXMLStreamReader(new StreamSource(entityStream));
            }
            unmarshaledResource = unmarshaller.unmarshal(xsrclassToFill);
        } 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(JAXBElement<?> t,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType) {
        return -1;
    }
    public boolean isWriteable(Class<?> type,
                               Type genericType,
                               Annotation[] annotations,
                               MediaType mediaType) {
        return  && isJAXBElement(typegenericType);
    }
    public void writeTo(JAXBElement<?> t,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType,
                        MultivaluedMap<StringObjecthttpHeaders,
                        OutputStream entityStreamthrows IOExceptionWebApplicationException {
        try {
            mediaType = MediaTypeUtils.setDefaultCharsetOnMediaTypeHeader(httpHeadersmediaType);
            Class<?> declaredType = t.getDeclaredType();
            JAXBContext context = getContext(declaredTypemediaType);
            Marshaller marshaller = getJAXBMarshaller(declaredTypecontextmediaType);
            Charset charset = Charset.forName(ProviderUtils.getCharset(mediaType));
            OutputStreamWriter writer = new OutputStreamWriter(entityStreamcharset);
            XMLStreamWriter xsw = null;
            if () {
                xsw = new BadgerFishXMLStreamWriter(writer);
            } else {
                try {
                    xsw =
                        new MappedXMLOutputFactory()
                            .createXMLStreamWriter(writer);
                } catch (XMLStreamException e) {
                    if (.isErrorEnabled()) {
                        .error(Messages.getMessage("jaxbFailToMarshal"t.getName()), e); //$NON-NLS-1$
                    }
                    throw new WebApplicationException(e..);
                }
            }
            marshaller.marshal(txsw);
            writer.flush();
        } catch (JAXBException e) {
            if (.isErrorEnabled()) {
                .error(Messages.getMessage("jaxbFailToMarshal"t.getName())); //$NON-NLS-1$
            }
            throw new WebApplicationException(e..);
        }
    }
New to GrepCode? Check out our FAQ X