KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > communication > sms > util > ClientHttpSocket


1 /**
2  * Copyright 2001-2004 The eXo platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5 package org.exoplatform.services.communication.sms.util;
6
7 import java.io.ByteArrayInputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.net.URL JavaDoc;
11
12 import org.apache.commons.httpclient.Header;
13 import org.apache.commons.httpclient.HttpClient;
14 import org.apache.commons.httpclient.HttpException;
15 import org.apache.commons.httpclient.HttpStatus;
16 import org.apache.commons.httpclient.NameValuePair;
17 import org.apache.commons.httpclient.methods.PostMethod;
18 import org.exoplatform.services.communication.sms.common.CommunicationError;
19
20
21 /**
22  * @author Ove Ranheim (oranheim@yahoo.no)
23  * @since Jul 6, 2004 1:15:03 AM
24  */

25 public class ClientHttpSocket {
26
27     private String JavaDoc _host;
28     private int _port;
29     private String JavaDoc _username;
30     private String JavaDoc _password;
31
32     public ClientHttpSocket(String JavaDoc host, int port, String JavaDoc username, String JavaDoc password) {
33         _host = host;
34         _port = port;
35         _username = username;
36         _password = password;
37     }
38
39     /**
40      * @param payload
41      * @return
42      */

43     public String JavaDoc sendMessage(String JavaDoc payload) throws CommunicationError {
44         String JavaDoc response = "";
45         HttpClient client = new HttpClient();
46         InputStream JavaDoc stream = new ByteArrayInputStream JavaDoc(payload.getBytes());
47         try {
48             URL JavaDoc url = new URL JavaDoc("http", _host, _port, "/sms");
49             PostMethod post = new PostMethod(url.toString());
50             try {
51                 post.addRequestHeader(new Header("Content-type", " application/xml"));
52                 post.addParameter(new NameValuePair("user", _username));
53                 post.addParameter(new NameValuePair("pw", _password));
54                 post.setRequestBody(stream);
55                 
56                 client.executeMethod(post);
57                 if (post.getStatusCode() == HttpStatus.SC_OK) {
58                     response = post.getResponseBodyAsString();
59                 } else {
60                     throw new CommunicationError("Unexpected failure: " + post.getStatusLine().toString());
61                 }
62             } finally {
63                 post.releaseConnection();
64             }
65             stream.close();
66         } catch (HttpException e) {
67             throw new CommunicationError(e);
68         } catch (IOException JavaDoc e) {
69             throw new CommunicationError(e);
70         }
71         return response;
72     }
73
74 }
Popular Tags