All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.security.oauth2.http.converter.jaxb.AbstractJaxbMessageConverter Maven / Gradle / Ivy

There is a newer version: 2.5.2.RELEASE
Show newest version
/*
 * Copyright 2011-2012 the original author or authors.
 *
 * 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
 *
 * https://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.springframework.security.oauth2.http.converter.jaxb;

import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.MarshalException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.UnmarshalException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.Result;
import javax.xml.transform.Source;

import org.springframework.http.HttpHeaders;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter;

/**
 * 

* Used as a convenience for converting an external object into an Object that can be marshalled using JAXB. *

*

* Note that the existing {@link AbstractXmlHttpMessageConverter} implementations will not work due to final methods * preventing the modification of the {@link Marshaller}. *

* * @author Rob Winch * * @param The internal representation of the object that can be safely marshalled/unmarshalled using JAXB. * @param The external representation of the object that is exposed externally but cannot be marshalled/unmarshalled using JAXB. */ abstract class AbstractJaxbMessageConverter extends AbstractXmlHttpMessageConverter { private final Class internalClass; private final Class externalClass; private final JAXBContext context; public AbstractJaxbMessageConverter(Class internalClass, Class externalClass) { this.internalClass = internalClass; this.externalClass = externalClass; try { context = JAXBContext.newInstance(this.internalClass); } catch (JAXBException e) { throw new RuntimeException(e); } } @Override protected final E readFromSource(Class clazz, HttpHeaders headers, Source source) throws IOException { try { JAXBElement jaxbElement = createUnmarshaller().unmarshal(source, internalClass); return convertToExternal(jaxbElement.getValue()); } catch (UnmarshalException ex) { throw new HttpMessageNotReadableException("Could not unmarshal to [" + clazz + "]: " + ex.getMessage(), ex); } catch (JAXBException ex) { throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex); } } @Override protected final void writeToResult(E accessToken, HttpHeaders headers, Result result) throws IOException { I convertedAccessToken = convertToInternal(accessToken); try { createMarshaller().marshal(convertedAccessToken, result); } catch (MarshalException ex) { throw new HttpMessageNotWritableException("Could not marshal [" + accessToken + "]: " + ex.getMessage(), ex); } catch (JAXBException ex) { throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex); } } @Override protected final boolean supports(Class clazz) { return this.externalClass.isAssignableFrom(clazz); } protected abstract E convertToExternal(I internalValue); protected abstract I convertToInternal(E externalValue); private Unmarshaller createUnmarshaller() throws JAXBException { return context.createUnmarshaller(); } private Marshaller createMarshaller() throws JAXBException { Marshaller marshaller = context.createMarshaller(); marshaller.setProperty("jaxb.fragment", Boolean.TRUE); return marshaller; } }