001: /* ====================================================================
002: * The LateralNZ Software License, Version 1.0
003: *
004: * Copyright (c) 2003 LateralNZ. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by
021: * LateralNZ (http://www.lateralnz.org/) and other third parties."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "LateralNZ" must not be used to endorse or promote
026: * products derived from this software without prior written
027: * permission. For written permission, please
028: * contact oss@lateralnz.org.
029: *
030: * 5. Products derived from this software may not be called "Panther",
031: * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
032: * "LATERALNZ" appear in their name, without prior written
033: * permission of LateralNZ.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of LateralNZ. For more
051: * information on Lateral, please see http://www.lateralnz.com/ or
052: * http://www.lateralnz.org
053: *
054: */
055: package org.lateralnz.common.util;
056:
057: import java.io.BufferedReader;
058: import java.io.InputStream;
059: import java.io.InputStreamReader;
060: import java.io.OutputStream;
061: import java.net.URL;
062: import java.net.HttpURLConnection;
063:
064: import org.apache.log4j.Logger;
065:
066: import org.lateralnz.common.util.Constants;
067:
068: /**
069: * standard HTTP functions
070: *
071: * @author J R Briggs
072: */
073: public class HttpUtils implements Constants {
074:
075: private static final Logger log = Logger.getLogger(HttpUtils.class
076: .getName());
077:
078: public static final int GET_METHOD = 0;
079: public static final int POST_METHOD = 1;
080: public static final String POST = "POST";
081: public static final String GET = "GET";
082: private static final String CLOSE = "Close";
083: private static final String CONNECTION = "Connection";
084: private static final String CONTENT_TYPE = "Content-Type";
085: private static final String CONTENT_LEN = "Content-Length"; // john, 27/08/2003.
086:
087: private static final String SENT_TEXT1 = "Sent :: ";
088: private static final String SENT_TEXT2 = " (";
089: private static final String SENT_TEXT3 = ")";
090:
091: private HttpUtils() {
092: }
093:
094: public static final String send(String url, int method,
095: String contentType, String body) throws Exception {
096: return send(url, method, null, contentType, body);
097: }
098:
099: /**
100: * send a 'request' to a remote server
101: * @param url the url to call. e.g. http://localhost:9090/controller
102: * @param method one of HttpUtils.GET_METHOD or HttpUtils.POST_METHOD
103: * @param contentType the content type of the request (e.g. text/xml)
104: * @param body in the case of a POST, this will contain the body of the post
105: */
106: public static final String send(String url, int method,
107: String[][] extraHeaders, String contentType, String body)
108: throws Exception {
109:
110: // replace spaces with '+' for the url. -- jwang
111: if (!StringUtils.isEmpty(url)) {
112: url = StringUtils.replace(url, SPACE, PLUS);
113: }
114:
115: String sentMethod;
116: URL messageURL = new URL(url);
117: HttpURLConnection con = null;
118: OutputStream out = null;
119: InputStream in = null;
120: BufferedReader r = null;
121:
122: try {
123: con = (HttpURLConnection) messageURL.openConnection();
124:
125: con.setRequestProperty(CONNECTION, CLOSE);
126: con.setDoInput(true);
127:
128: if (extraHeaders != null) {
129: for (int i = 0; i < extraHeaders.length; i++) {
130: con.setRequestProperty(extraHeaders[i][0],
131: extraHeaders[i][1]);
132: }
133: }
134:
135: if (method == GET_METHOD) {
136: con.setRequestMethod(GET);
137: con.setDoOutput(false);
138: sentMethod = GET;
139: } else {
140: con.setRequestMethod(POST);
141: con.setDoOutput(true);
142: con.setRequestProperty(CONTENT_TYPE, contentType);
143:
144: // jwang, 27/08/2003.
145: if (!StringUtils.isEmpty(body)) {
146: int length = body.length();
147: con.setRequestProperty(CONTENT_LEN, EMPTY + length);
148: } else {
149: con.setRequestProperty(CONTENT_LEN, ZERO);
150: }
151:
152: out = con.getOutputStream();
153: out.write(body.getBytes());
154: sentMethod = POST;
155: }
156:
157: in = con.getInputStream();
158: r = new BufferedReader(new InputStreamReader(in));
159: String line;
160:
161: StringBuffer sb = new StringBuffer();
162: char[] buf = new char[1024];
163: int len;
164: while ((len = r.read(buf)) != -1) {
165: sb.append(buf, 0, len);
166: }
167:
168: // assuming log4j has been correctly setup, this will go to another file
169: // from the main server log
170: //log.debug(SENT_TEXT1 + url + SENT_TEXT2 + sentMethod + SENT_TEXT3 + NEWLINE + body + NEWLINE + NEWLINE);
171: log.info("Sent: " + url);
172: log.info("Method: " + sentMethod);
173: //log.info("Body: " + body + NEWLINE + NEWLINE);
174:
175: return sb.toString();
176: } finally {
177: IOUtils.close(r);
178: IOUtils.close(in);
179: IOUtils.close(out);
180: IOUtils.disconnect(con);
181: }
182: }
183:
184: private static final int getMethod(String method) throws Exception {
185: if (method != null) {
186: if (method.equals(GET)) {
187: return GET_METHOD;
188: } else if (method.equals(POST)) {
189: return POST_METHOD;
190: }
191: }
192: throw new Exception("invalid method : " + method);
193: }
194:
195: // invokable from the command line
196: public static final void main(String[] args) throws Exception {
197: String url = args[0];
198: int method = getMethod(args[1]);
199: String contentType = args[2];
200: String body = StringUtils.readFromFile(args[3]);
201:
202: if (StringUtils.isEmpty(body)) {
203: throw new Exception("invalid file " + args[3]);
204: }
205:
206: System.out.println(HttpUtils.send(url, method, contentType,
207: body));
208: }
209: }
|