| 1 |
/* |
| 2 |
* Copyright 2001-2004 The Apache Software Foundation. |
| 3 |
* |
| 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 |
| 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, |
| 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. |
| 15 |
*/ |
| 16 |
|
| 17 |
package samples.swa; |
| 18 |
|
| 19 |
import javax.activation.DataHandler; |
| 20 |
import javax.activation.FileDataSource; |
| 21 |
import javax.mail.internet.MimeBodyPart; |
| 22 |
import javax.mail.internet.MimeMultipart; |
| 23 |
|
| 24 |
/** |
| 25 |
* Class Tester |
| 26 |
* |
| 27 |
* @version %I%, %G% |
| 28 |
*/ |
| 29 |
public class Tester { |
| 30 |
|
| 31 |
/** Field HEADER_CONTENT_TYPE */ |
| 32 |
public static final String HEADER_CONTENT_TYPE = "Content-Type"; |
| 33 |
|
| 34 |
/** Field HEADER_CONTENT_TRANSFER_ENCODING */ |
| 35 |
public static final String HEADER_CONTENT_TRANSFER_ENCODING = |
| 36 |
"Content-Transfer-Encoding"; |
| 37 |
|
| 38 |
/** Field address */ |
| 39 |
private static final java.lang.String address = |
| 40 |
"http://localhost:8080/axis/services/SwaHttp"; |
| 41 |
|
| 42 |
/** |
| 43 |
* Method main |
| 44 |
* |
| 45 |
* @param args |
| 46 |
* @throws Exception |
| 47 |
*/ |
| 48 |
public static void main(String[] args) throws Exception { |
| 49 |
|
| 50 |
/* |
| 51 |
* Start to prepare service call. Once this is done, several |
| 52 |
* calls can be made on the port (see below) |
| 53 |
* |
| 54 |
* Fist: get the service locator. This implements the functionality |
| 55 |
* to get a client stub (aka port). |
| 56 |
*/ |
| 57 |
SwaServiceLocator service = new SwaServiceLocator(); |
| 58 |
|
| 59 |
/* |
| 60 |
* Here we use an Axis specific call that allows to override the |
| 61 |
* port address (service endpoint address) with an own URL. Comes |
| 62 |
* in handy for testing. |
| 63 |
*/ |
| 64 |
java.net.URL endpoint; |
| 65 |
try { |
| 66 |
endpoint = new java.net.URL(address); |
| 67 |
} catch (java.net.MalformedURLException e) { |
| 68 |
throw new javax.xml.rpc.ServiceException(e); |
| 69 |
} |
| 70 |
|
| 71 |
SwaPort port = (SwaPort) service.getSwaHttp(endpoint); |
| 72 |
|
| 73 |
/* |
| 74 |
* At this point all preparations are done. Using the port we can |
| 75 |
* now perform as many calls as necessary. |
| 76 |
*/ |
| 77 |
|
| 78 |
/* |
| 79 |
* Prepare the Multipart attachment. It consists of several data files. The |
| 80 |
* multipart container is of type "multipart/mixed" |
| 81 |
*/ |
| 82 |
MimeMultipart mpRoot = new MimeMultipart(); |
| 83 |
System.out.println("MimeMultipart content: " + mpRoot.getContentType()); |
| 84 |
DataHandler dh = new DataHandler(new FileDataSource("duke.gif")); |
| 85 |
addBodyPart(mpRoot, dh); |
| 86 |
dh = new DataHandler(new FileDataSource("pivots.jpg")); |
| 87 |
addBodyPart(mpRoot, dh); |
| 88 |
// perform call |
| 89 |
port.swaSend("AppName", mpRoot); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Method addBodyPart |
| 94 |
* |
| 95 |
* @param mp |
| 96 |
* @param dh |
| 97 |
*/ |
| 98 |
private static void addBodyPart(MimeMultipart mp, DataHandler dh) { |
| 99 |
MimeBodyPart messageBodyPart = new MimeBodyPart(); |
| 100 |
try { |
| 101 |
messageBodyPart.setDataHandler(dh); |
| 102 |
String contentType = dh.getContentType(); |
| 103 |
if ((contentType == null) || (contentType.trim().length() == 0)) { |
| 104 |
contentType = "application/octet-stream"; |
| 105 |
} |
| 106 |
System.out.println("Content type: " + contentType); |
| 107 |
messageBodyPart.setHeader(HEADER_CONTENT_TYPE, contentType); |
| 108 |
messageBodyPart.setHeader( |
| 109 |
HEADER_CONTENT_TRANSFER_ENCODING, |
| 110 |
"binary"); // Safe and fastest for anything other than mail |
| 111 |
mp.addBodyPart(messageBodyPart); |
| 112 |
} catch (javax.mail.MessagingException e) { |
| 113 |
} |
| 114 |
} |
| 115 |
} |