Java tutorial
/* * Copyright (c) 2014 Maciej Walkowiak * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package pl.maciejwalkowiak.plist.spring; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; import org.springframework.http.converter.AbstractHttpMessageConverter; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotWritableException; import pl.maciejwalkowiak.plist.PlistSerializer; import pl.maciejwalkowiak.plist.PlistSerializerImpl; import java.io.IOException; import java.io.OutputStreamWriter; import java.nio.charset.Charset; public class PlistHttpMessageConverter extends AbstractHttpMessageConverter<Object> { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); private PlistSerializer plistSerializer; private boolean addHeader; /** * Construct a new {@code PlistHttpMessageConverter}. */ public PlistHttpMessageConverter(boolean addHeader) { super(new MediaType("application", "x-plist", DEFAULT_CHARSET), new MediaType("application", "x-apple-aspen-mdm-checkin", DEFAULT_CHARSET)); this.addHeader = addHeader; plistSerializer = new PlistSerializerImpl(); } @Override protected boolean supports(Class<?> clazz) { throw new UnsupportedOperationException(); } @Override public boolean canRead(Class<?> clazz, MediaType mediaType) { return false; } @Override public boolean canWrite(Class<?> clazz, MediaType mediaType) { return canWrite(mediaType); } @Override protected Object readInternal(Class<?> aClass, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException { return null; } private Charset getCharset(HttpHeaders headers) { if (headers == null || headers.getContentType() == null || headers.getContentType().getCharSet() == null) { return DEFAULT_CHARSET; } return headers.getContentType().getCharSet(); } @Override protected void writeInternal(Object o, HttpOutputMessage httpOutputMessage) throws IOException, HttpMessageNotWritableException { Charset charset = getCharset(httpOutputMessage.getHeaders()); OutputStreamWriter writer = new OutputStreamWriter(httpOutputMessage.getBody(), charset); String result = addHeader ? plistSerializer.toXmlPlist(o) : plistSerializer.serialize(o); writer.write(result); writer.close(); } }