01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.axis2.jaxws.provider.jaxb;
20:
21: import java.io.ByteArrayInputStream;
22: import java.io.ByteArrayOutputStream;
23:
24: import javax.xml.bind.JAXBContext;
25: import javax.xml.bind.Marshaller;
26: import javax.xml.bind.Unmarshaller;
27: import javax.xml.transform.Source;
28: import javax.xml.transform.stream.StreamSource;
29: import javax.xml.ws.Provider;
30: import javax.xml.ws.WebServiceProvider;
31:
32: import org.test.mtom.ObjectFactory;
33: import org.test.mtom.SendImage;
34: import org.test.mtom.SendImageResponse;
35: import org.apache.axis2.jaxws.TestLogger;
36:
37: /**
38: * A JAXWS Source Provider implementation
39: *
40: */
41: @WebServiceProvider()
42: public class JAXBProvider implements Provider<Source> {
43:
44: /**
45: * Required impl method from javax.xml.ws.Provider interface
46: * @param obj
47: * @return
48: */
49: public Source invoke(Source obj) {
50: TestLogger.logger
51: .debug(">> JAXB Provider Service: Request received.\n");
52: SendImage siRequest = null;
53: SendImageResponse siResponse = null;
54: StreamSource streamSource = null;
55:
56: try {
57: //Create a request object
58: siRequest = new ObjectFactory().createSendImage();
59:
60: //Unmarshall recieved Source to get request param.
61: JAXBContext jbc = JAXBContext.newInstance("org.test.mtom");
62: Unmarshaller um = jbc.createUnmarshaller();
63: siRequest = (SendImage) um.unmarshal(obj);
64:
65: //Create a response object
66: siResponse = new ObjectFactory().createSendImageResponse();
67: siResponse.setOutput(siRequest.getInput());
68:
69: //Marshall the response object and create a StreamSource from the
70: //resulting byte array input stream
71: Marshaller m = jbc.createMarshaller();
72: ByteArrayOutputStream baos = new ByteArrayOutputStream();
73: m.marshal(siResponse, baos);
74: byte[] bite = baos.toByteArray();
75: ByteArrayInputStream bais = new ByteArrayInputStream(bite);
76: streamSource = new StreamSource(bais);
77: } catch (Exception e) {
78: e.printStackTrace();
79: }
80:
81: return streamSource;
82:
83: }
84: }
|