com.sun.jersey.atom.abdera.ContentHelper Maven / Gradle / Ivy
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.sun.jersey.atom.abdera;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.annotation.Annotation;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Providers;
import org.apache.abdera.Abdera;
import org.apache.abdera.model.Document;
import org.apache.abdera.model.Element;
import org.apache.abdera.model.Entry;
import org.apache.abdera.parser.Parser;
/**
* Helper class to assist in serializing and deserializing Java entities
* as XML that will be transferred through the content
element
* of an Atom Entry
instance. A configured instance of this
* class can be made available in a resource class as follows:
*
*
* @Context
* private ContentHelper contentHelper;
*
*/
public class ContentHelper {
// ------------------------------------------------------------ Constructors
/**
* Construct a configured instance of this helper. * * @param providers Providers for this application */ public ContentHelper(Providers providers) { this.providers = providers; } // ------------------------------------------------------ Instance Variables /** *
Singleton Abdera instance for this application.
*/ private Abdera abdera = Abdera.getInstance(); /** *Empty array of annotations for calls to message body readers and writers.
*/ private Annotation[] emptyAnnotations = new Annotation[0]; /** *Empty map of headers for calls to message body readers and writers.
*/ private MultivaluedMapThe injected helper to look up appropriate Provider
* instances.
Deserialize the content element of the specified entry, and
* transform it back into an appropriate Java object. The media type
* used for selecting an appropriate Provider
will be
* acquired from the type
attribute of the content
* element.
Entry
whose content element is to be processed
* @param clazz Class
of the object to be returned
*
* @exception IllegalArgumentException if the specified entry does not
* contain a valid content element
*/
public Deserialize the content element of the specified entry, and * transform it back into an appropriate Java object.
* * @param entryEntry
whose content element is to be processed
* @param mediaType MediaType
to use when selecting an
* appropriate provider
* @param clazz Class
of the object to be returned
*
* @exception IllegalArgumentException if the specified entry does not
* contain a valid content element
*/
public Serialize the specified entity as the content
element
* of the specified entry
. The selected provider MUST
* produce an XML representation.
Entry
whose content element is to be set
* @param mediaType MediaType
to pass as the type
* attribute of the content
element (also used to select an
* appropriate Provider
)
* @param entity Entity to be serialized
*/
public void setContentEntity(Entry entry, MediaType mediaType, Object entity) {
// Select the MessageBodyWriter we will use
MessageBodyWriter writer =
providers.getMessageBodyWriter(entity.getClass(),
entity.getClass(),
emptyAnnotations,
mediaType);
if (writer == null) {
throw new IllegalArgumentException
("No MessageBodyWriter for class '" +
entity.getClass().getName() + "' and media type '" + mediaType + "'");
}
// Serialize this entity to a byte stream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
writer.writeTo(entity, entity.getClass(), entity.getClass(),
emptyAnnotations, mediaType, emptyHeaders, baos);
} catch (IOException e) {
// Can not happen
}
// Parse the XML into an Abdera Element (yes, this is pretty smelly)
// and set it as the content element
Parser parser = abdera.getParser();
Document document = parser.parse(new ByteArrayInputStream(baos.toByteArray()));
entry.setContent(document.getRoot(), mediaType.toString());
}
}