File: Tester.java

package info (click to toggle)
axis 1.4-25
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 51,864 kB
  • sloc: java: 129,119; xml: 10,573; jsp: 983; sh: 84; cs: 36; makefile: 26
file content (115 lines) | stat: -rw-r--r-- 3,892 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
/*
 * 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.swa;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;

/**
 * Class Tester
 * 
 * @version %I%, %G%
 */
public class Tester {

    /** Field HEADER_CONTENT_TYPE */
    public static final String HEADER_CONTENT_TYPE = "Content-Type";

    /** Field HEADER_CONTENT_TRANSFER_ENCODING */
    public static final String HEADER_CONTENT_TRANSFER_ENCODING =
            "Content-Transfer-Encoding";

    /** Field address */
    private static final java.lang.String address =
            "http://localhost:8080/axis/services/SwaHttp";

    /**
     * Method main
     * 
     * @param args 
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {

        /*
         * Start to prepare service call. Once this is done, several
         * calls can be made on the port (see below)
         *
         * Fist: get the service locator. This implements the functionality
         * to get a client stub (aka port).
         */
        SwaServiceLocator service = new SwaServiceLocator();

        /*
         * Here we use an Axis specific call that allows to override the
         * port address (service endpoint address) with an own URL. Comes
         * in handy for testing.
         */
        java.net.URL endpoint;
        try {
            endpoint = new java.net.URL(address);
        } catch (java.net.MalformedURLException e) {
            throw new javax.xml.rpc.ServiceException(e);
        }

        SwaPort port = (SwaPort) service.getSwaHttp(endpoint);

        /*
         * At this point all preparations are done. Using the port we can
         * now perform as many calls as necessary.
         */

        /*
         * Prepare the Multipart attachment. It consists of several data files. The
         * multipart container is of type "multipart/mixed"
         */
        MimeMultipart mpRoot = new MimeMultipart();
        System.out.println("MimeMultipart content: " + mpRoot.getContentType());
        DataHandler dh = new DataHandler(new FileDataSource("duke.gif"));
        addBodyPart(mpRoot, dh);
        dh = new DataHandler(new FileDataSource("pivots.jpg"));
        addBodyPart(mpRoot, dh);
        // perform call
        port.swaSend("AppName", mpRoot);
    }

    /**
     * Method addBodyPart
     * 
     * @param mp 
     * @param dh 
     */
    private static void addBodyPart(MimeMultipart mp, DataHandler dh) {
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        try {
            messageBodyPart.setDataHandler(dh);
            String contentType = dh.getContentType();
            if ((contentType == null) || (contentType.trim().length() == 0)) {
                contentType = "application/octet-stream";
            }
            System.out.println("Content type: " + contentType);
            messageBodyPart.setHeader(HEADER_CONTENT_TYPE, contentType);
            messageBodyPart.setHeader(
                    HEADER_CONTENT_TRANSFER_ENCODING,
                    "binary");    // Safe and fastest for anything other than mail
            mp.addBodyPart(messageBodyPart);
        } catch (javax.mail.MessagingException e) {
        }
    }
}