2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.server
;
19 import java
.io
.IOException
;
20 import java
.io
.Writer
;
22 import javax
.servlet
.http
.HttpServletRequest
;
23 import javax
.servlet
.http
.HttpServletResponse
;
24 import javax
.xml
.transform
.stream
.StreamResult
;
26 import org
.argeo
.ArgeoException
;
27 import org
.springframework
.oxm
.Marshaller
;
29 public class MarshallerServerSerializer
implements ServerSerializer
, Serializer
{
30 private Marshaller marshaller
;
31 private String contentTypeCharset
= "UTF-8";
33 @SuppressWarnings("restriction")
34 public void serialize(Object obj
, HttpServletRequest request
,
35 HttpServletResponse response
) {
36 response
.setContentType("text/xml;charset=" + contentTypeCharset
);
38 serialize(obj
, response
.getWriter());
39 } catch (IOException e
) {
40 throw new ArgeoException("Cannot serialize " + obj
, e
);
44 public void serialize(Object obj
, Writer writer
) {
46 StreamResult result
= new StreamResult(writer
);
47 marshaller
.marshal(obj
, result
);
48 } catch (Exception e
) {
49 throw new ArgeoException("Cannot serialize " + obj
, e
);
54 public void serialize(Writer writer
, Object obj
) {
55 serialize(obj
, writer
);
58 public void setMarshaller(Marshaller marshaller
) {
59 this.marshaller
= marshaller
;
62 public void setContentTypeCharset(String contentTypeCharset
) {
63 this.contentTypeCharset
= contentTypeCharset
;