File: TestRef.java

package info (click to toggle)
axis 1.4-28%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 52,096 kB
  • sloc: java: 129,124; xml: 10,602; jsp: 983; sh: 84; cs: 36; makefile: 18
file content (361 lines) | stat: -rw-r--r-- 14,448 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package samples.attachments;


import org.apache.axis.AxisFault;
import org.apache.axis.attachments.AttachmentPart;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.SOAPBodyElement;
import org.apache.axis.transport.http.HTTPConstants;
import org.apache.axis.utils.Options;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import java.io.ByteArrayInputStream;
import java.net.URL;
import java.util.Hashtable;
import java.util.Vector;


/**
 *
 * @author Rick Rineholt 
 */

/**
 * An example of sending an attachment via messages.
 *  The main purpose is to validate the different types of attachment references
 *  by content Id, content location both absolute and relative.
 * 
 *  Creates 5 separate attachments referenced  differently by a SOAP document.
 *  Each attachment contains a string that is assembled and tested to see if
 *  if the attachments are correctly sent and referenced.  Each attachment also
 *  contains a mime header indicating its position and validated on the server
 *  to see if mime headers are correctly sent with attachments.
 *
 *  Sends the same message again however the second attachments are placed in the
 *  stream in reverse to see if they are still referenced ok.
 *
 *
 *  The return SOAP document references a single attachment which is the a Java
 *  serialized vector holding strings to the individual attachments sent.
 *
 *  Demos using attachments directly.
 *  
 */
public class TestRef {

    Options opts = null;
    public static final String positionHTTPHeader="Ordinal";
    public static final String TheKey= "Now is the time for all good men to come to the aid of their country.";


    public TestRef( Options opts) {
        this.opts = opts;
    }


    /**
     * This method sends all the files in a directory. 
     *  @param The directory that is the source to send.
     *  @return True if sent and compared.
     */
    public boolean testit() throws Exception {
        boolean rc = true;
        String baseLoc= "http://axis.org/attachTest";
        Vector refs= new Vector();  //holds a string of references to attachments.

        Service  service = new Service(); //A new axis Service.

        Call     call    = (Call) service.createCall(); //Create a call to the service.

        /*Un comment the below statement to do HTTP/1.1 protocol*/
      //call.setScopedProperty(MessageContext.HTTP_TRANSPORT_VERSION,HTTPConstants.HEADER_PROTOCOL_V11);
        Hashtable myhttp= new Hashtable();
        myhttp.put(HTTPConstants.HEADER_CONTENT_LOCATION, baseLoc);     //Send extra soap headers

        /*Un comment the below to do http chunking to avoid the need to calculate content-length. (Needs HTTP/1.1)*/
      //myhttp.put(HTTPConstants.HEADER_TRANSFER_ENCODING, HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED);

        /*Un comment the below to force a 100-Continue... This will cause  httpsender to wait for
         * this response on a post.  If HTTP 1.1 and this is not set, *SOME* servers *MAY* reply with this anyway.
         *  Currently httpsender won't handle this situation, this will require the resp. which it will handle.
         */
      //myhttp.put(HTTPConstants.HEADER_EXPECT, HTTPConstants.HEADER_EXPECT_100_Continue);
        call.setProperty(HTTPConstants.REQUEST_HEADERS,myhttp);

        call.setTargetEndpointAddress( new URL(opts.getURL()) ); //Set the target service host and service location, 

        java.util.Stack rev= new java.util.Stack();
        //Create an attachment referenced by a generated contentId.
        AttachmentPart ap= new AttachmentPart(new javax.activation.DataHandler(
          "Now is the time", "text/plain" ));
         refs.add(ap.getContentIdRef()); //reference the attachment by contentId.
         ap.setMimeHeader(positionHTTPHeader, ""+refs.size() ); //create a MIME header indicating postion.
        call.addAttachmentPart(ap);
        rev.push(ap);

        //Create an attachment referenced by a set contentId.
        String setContentId="rick_did_this";
        ap= new AttachmentPart(new DataHandler(" for all good", "text/plain" ));
          //new MemoryOnlyDataSource(
         ap.setContentId(setContentId);  
         refs.add("cid:" + setContentId); //reference the attachment by contentId.
         ap.setMimeHeader(positionHTTPHeader, ""+refs.size() ); //create a MIME header indicating postion.
        call.addAttachmentPart(ap);
        rev.push(ap);

        //Create an attachment referenced by a absolute contentLocation.
        ap= new AttachmentPart(new DataHandler( " men to", "text/plain" ));
          //new MemoryOnlyDataSource( " men to", "text/plain" )));
        ap.setContentLocation(baseLoc+ "/firstLoc");  
         refs.add(baseLoc+ "/firstLoc"); //reference the attachment by contentId.
         ap.setMimeHeader(positionHTTPHeader, ""+refs.size() ); //create a MIME header indicating postion.
        call.addAttachmentPart(ap);
        rev.push(ap);

        //Create an attachment referenced by relative location to a absolute location. 
        ap= new AttachmentPart(new DataHandler( " come to", "text/plain" ));
          // new MemoryOnlyDataSource( " come to", "text/plain" )));
        ap.setContentLocation(baseLoc+ "/secondLoc");  
        refs.add("secondLoc"); //reference the attachment by contentId.
        ap.setMimeHeader(positionHTTPHeader, ""+refs.size() ); //create a MIME header indicating postion.
        call.addAttachmentPart(ap);
        rev.push(ap);

        //Create an attachment referenced by relative location to a relative location. 
        ap= new AttachmentPart(new DataHandler( " the aid of their country.", "text/plain" ));
          // new MemoryOnlyDataSource( " the aid of their country.", "text/plain" )));
        ap.setContentLocation("thirdLoc");  
        refs.add("thirdLoc"); //reference the attachment by contentId.
        ap.setMimeHeader(positionHTTPHeader, ""+refs.size() ); //create a MIME header indicating postion.
        call.addAttachmentPart(ap);
        rev.push(ap);


        //Now build the message....
        String namespace="urn:attachmentsTestRef"; //needs to match name of service. 

        StringBuffer msg = new StringBuffer("\n<attachments xmlns=\"" +namespace +"\">\n");
        for (java.util.Iterator i = refs.iterator(); i.hasNext() ; )
          msg.append("    <attachment href=\"" + (String) i.next()  + "\"/>\n");

        msg.append(  "</attachments>");

        call.setUsername( opts.getUser());

        call.setPassword( opts.getPassword() );

        call.setOperationStyle("document");
        call.setOperationUse("literal");

        //Now do the call....
        Object ret = call.invoke(new Object[]{
           new SOAPBodyElement(new ByteArrayInputStream(msg.toString().getBytes("UTF-8"))) } ); 

        validate(ret, call, "12345");   

        //Note: that even though the attachments are sent in reverse they are still
        // retreived by reference so the ordinal will still match.
        int revc=1;
        for( ap= (AttachmentPart)rev.pop(); ap!=null ;ap= rev.empty()? null :  (AttachmentPart)rev.pop()){
          call.addAttachmentPart(ap);
        }

        //Now do the call....
        ret = call.invoke(new Object[]{
           new SOAPBodyElement(new ByteArrayInputStream(msg.toString().getBytes("UTF-8"))) } ); 

        validate(ret, call, "54321");   


        return rc;
    }

    void validate(Object ret, Call call, final String expOrdPattern) throws Exception{   
        if (null == ret) {
            System.out.println("Received null ");
            throw new AxisFault("", "Received null", null, null);
        }

        if (ret instanceof String) {
            System.out.println("Received problem response from server: " + ret);
            throw new AxisFault("", (String) ret, null, null);
        }

        Vector vret= (Vector) ret;

        if (!(ret instanceof java.util.Vector )) {
            //The wrong type of object that what was expected.
            System.out.println("Received unexpected type :" +
                ret.getClass().getName());
            throw new AxisFault("", "Received unexpected type:" +
                    ret.getClass().getName(), null, null);

        }

        org.apache.axis.message.RPCElement retrpc= (org.apache.axis.message.RPCElement )
          ((Vector)ret).elementAt(0);

        Document retDoc= org.apache.axis.utils.XMLUtils.newDocument(
          new org.xml.sax.InputSource(new java.io.ByteArrayInputStream(
            retrpc.toString().getBytes())));

        //get at the attachments.    
        org.apache.axis.attachments.Attachments attachments= 
          call.getResponseMessage().getAttachmentsImpl();

        //Still here, so far so good.
        Element rootEl= retDoc.getDocumentElement();

        Element caEl= getNextFirstChildElement(rootEl);
        //this should be the only child element with the ref to our attachment
        // response.
        String href= caEl.getAttribute("href");
        org.apache.axis.Part p= attachments.getAttachmentByReference(href);
        if(null == p)
         throw new org.apache.axis.AxisFault("Attachment for ref='"+href+"' not found." );

         //Check to see the the attachment were sent in order
        String ordPattern= caEl.getAttribute("ordinalPattern");
        if(!expOrdPattern.equals(ordPattern))
          throw new org.apache.axis.AxisFault(
           "Attachments sent out of order expected:'" +expOrdPattern + "', got:'"+ordPattern+"'."  );

         //now get at the data...
         DataHandler dh= ((org.apache.axis.attachments.AttachmentPart)p).getDataHandler();
         System.err.println("content-type:" + dh.getContentType());

       java.util.Vector rspVector= null;
       Object rspObject =  dh.getContent();//This SHOULD just return the vector but reality strikes...
       if(rspObject == null)
           throw new AxisFault("", "Received unexpected object:null", null, null);
       else if(rspObject instanceof java.util.Vector) rspVector= (java.util.Vector)rspObject;
       else if(rspObject instanceof java.io.InputStream)
          rspVector= (java.util.Vector)
           new java.io.ObjectInputStream((java.io.InputStream)rspObject ).readObject();
       else    
           throw new AxisFault("", "Received unexpected object:" +
                    rspObject.getClass().getName(), null, null);

      StringBuffer fullmsg= new StringBuffer();
      for(java.util.Iterator ri= rspVector.iterator(); ri.hasNext();){
        String part= (String)ri.next();
        fullmsg.append(part);
        System.out.print(part);
      }
      System.out.println("");

      if(!(TheKey.equals (fullmsg.toString())))
        throw new org.apache.axis.AxisFault("Fullmsg not correct'"+fullmsg +"'." );
    }

    Element getNextFirstChildElement(Node n) {
        if(n== null) return null;
        n= n.getFirstChild();
        for(; n!= null && !(n instanceof Element); n= n.getNextSibling());
        return (Element)n;
    }

    Element getNextSiblingElement(Node n) {
        if(n== null) return null;
        n= n.getNextSibling();
        for(; n!= null && !(n instanceof Element); n= n.getNextSibling());
        return (Element)n;
    }

    /**
     * Give a single file to send or name a directory
     * to send an array of attachments of the files in
     * that directory.
     */
    public static void main(String args[]) {
        try {

            Options opts = new Options(args);
            TestRef echoattachment = new TestRef(opts);

            args = opts.getRemainingArgs();
            int argpos=0;

            if (echoattachment.testit()) {
                System.out.println("Attachments sent and received ok!");
                System.exit(0);
            }
        }
        catch ( Exception e ) {
            System.err.println(e);
             e.printStackTrace();
        }
        System.exit(18);
    }


    /**
     *  Return an array of datahandlers for each file in the dir. 
     *  @param the name of the directory
     *  @return return an array of datahandlers.
     */

    protected DataHandler[] getAttachmentsFromDir(String dirName) {
        java.util.LinkedList retList = new java.util.LinkedList();
        DataHandler[] ret = new DataHandler[0];// empty

        java.io.File sourceDir = new java.io.File(dirName);

        java.io.File[] files = sourceDir.listFiles();

        for ( int i = files.length - 1; i >= 0; --i) {
            java.io.File cf = files[i];

            if (cf.isFile() && cf.canRead()) {
                String fname = null;

                try {
                    fname = cf.getAbsoluteFile().getCanonicalPath();
                }
                catch ( java.io.IOException e) {
                    System.err.println("Couldn't get file \"" + fname + "\" skipping...");
                    continue;
                }
                retList.add( new DataHandler( new FileDataSource( fname )));
            }
        }
        if (!retList.isEmpty()) {
            ret = new DataHandler[ retList.size()];
            ret = (DataHandler[]) retList.toArray(ret);
        }

        return ret;
    }

    /**This class should store all attachment data in memory */
    static class MemoryOnlyDataSource extends org.apache.axis.attachments.ManagedMemoryDataSource{
    
       MemoryOnlyDataSource( byte [] in, String contentType) throws java.io.IOException{
         super( new java.io.ByteArrayInputStream( in) , Integer.MAX_VALUE -2, contentType, true); 
       }
       MemoryOnlyDataSource( String in, String contentType)throws java.io.IOException{
         this( in.getBytes() ,  contentType); 
       }
    }
}