KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > mtom > client > MTOMClientModel


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package sample.mtom.client;
17
18 import org.apache.axis2.Constants;
19 import org.apache.axis2.addressing.AddressingConstants;
20 import org.apache.axis2.addressing.EndpointReference;
21 import org.apache.axis2.attachments.ImageDataSource;
22 import org.apache.axis2.attachments.JDK13IO;
23 import org.apache.axis2.clientapi.Call;
24 import org.apache.axis2.om.*;
25 import org.apache.axis2.om.impl.llom.OMTextImpl;
26
27 import javax.activation.DataHandler JavaDoc;
28 import javax.xml.namespace.QName JavaDoc;
29 import java.awt.*;
30 import java.io.File JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32
33
34 public class MTOMClientModel {
35     private File JavaDoc inputFile = null;
36
37     private EndpointReference targetEPR = new EndpointReference(AddressingConstants.WSA_TO,
38             "http://127.0.0.1:8080/axis2/services/MyService");
39
40     private QName JavaDoc operationName = new QName JavaDoc("mtomSample");
41
42
43     public MTOMClientModel() {
44
45     }
46
47     private OMElement createEnvelope(String JavaDoc fileName) throws Exception JavaDoc {
48
49         DataHandler JavaDoc expectedDH;
50         OMFactory fac = OMAbstractFactory.getOMFactory();
51         OMNamespace omNs = fac.createOMNamespace("http://localhost/my", "my");
52
53         OMElement data = fac.createOMElement("mtomSample", omNs);
54         OMElement image = fac.createOMElement("image", omNs);
55         Image expectedImage;
56         expectedImage = new JDK13IO()
57                 .loadImage(new FileInputStream JavaDoc(inputFile));
58
59         ImageDataSource dataSource = new ImageDataSource("test.jpg",
60                 expectedImage);
61         expectedDH = new DataHandler JavaDoc(dataSource);
62         OMText textData = fac.createText(expectedDH, true);
63         image.addChild(textData);
64
65         OMElement imageName = fac.createOMElement("fileName", omNs);
66         if (fileName != null) {
67             imageName.setText(fileName);
68         }
69         data.addChild(image);
70         data.addChild(imageName);
71
72         return data;
73
74     }
75
76     public OMElement testEchoXMLSync(String JavaDoc fileName) throws Exception JavaDoc {
77
78         OMElement payload = createEnvelope(fileName);
79
80         Call call = new Call();
81         call.setTo(targetEPR);
82         // enabling MTOM in the client side
83
call.set(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
84         call.setTransportInfo(Constants.TRANSPORT_HTTP,
85                 Constants.TRANSPORT_HTTP, false);
86         OMElement result = (OMElement) call.invokeBlocking(operationName
87                 .getLocalPart(), payload);
88
89         return result;
90     }
91
92
93     public void setTargetEPR(String JavaDoc targetEPR) {
94         this.targetEPR = new EndpointReference(AddressingConstants.WSA_TO,
95                 targetEPR);
96
97     }
98
99
100     public void setInputFile(File JavaDoc inputFile) {
101         this.inputFile = inputFile;
102     }
103 }
104
Popular Tags