]> git.argeo.org Git - lgpl/argeo-commons.git/blob - MarshallerServerSerializer.java
65ffebab5200747e68505860144882acbef43e0c
[lgpl/argeo-commons.git] / MarshallerServerSerializer.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.server;
18
19 import java.io.IOException;
20 import java.io.Writer;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24 import javax.xml.transform.stream.StreamResult;
25
26 import org.argeo.ArgeoException;
27 import org.springframework.oxm.Marshaller;
28
29 public class MarshallerServerSerializer implements ServerSerializer, Serializer {
30 private Marshaller marshaller;
31 private String contentTypeCharset = "UTF-8";
32
33 @SuppressWarnings("restriction")
34 public void serialize(Object obj, HttpServletRequest request,
35 HttpServletResponse response) {
36 response.setContentType("text/xml;charset=" + contentTypeCharset);
37 try {
38 serialize(obj, response.getWriter());
39 } catch (IOException e) {
40 throw new ArgeoException("Cannot serialize " + obj, e);
41 }
42 }
43
44 public void serialize(Object obj, Writer writer) {
45 try {
46 StreamResult result = new StreamResult(writer);
47 marshaller.marshal(obj, result);
48 } catch (Exception e) {
49 throw new ArgeoException("Cannot serialize " + obj, e);
50 }
51 }
52
53 @Deprecated
54 public void serialize(Writer writer, Object obj) {
55 serialize(obj, writer);
56 }
57
58 public void setMarshaller(Marshaller marshaller) {
59 this.marshaller = marshaller;
60 }
61
62 public void setContentTypeCharset(String contentTypeCharset) {
63 this.contentTypeCharset = contentTypeCharset;
64 }
65
66 }