01: /*
02: * BEGIN_HEADER - DO NOT EDIT
03: *
04: * The contents of this file are subject to the terms
05: * of the Common Development and Distribution License
06: * (the "License"). You may not use this file except
07: * in compliance with the License.
08: *
09: * You can obtain a copy of the license at
10: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
11: * See the License for the specific language governing
12: * permissions and limitations under the License.
13: *
14: * When distributing Covered Code, include this CDDL
15: * HEADER in each file and include the License file at
16: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
17: * If applicable add the following below this CDDL HEADER,
18: * with the fields enclosed by brackets "[]" replaced with
19: * your own identifying information: Portions Copyright
20: * [year] [name of copyright owner]
21: */
22:
23: /*
24: * @(#)Helper.java
25: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
26: *
27: * END_HEADER - DO NOT EDIT
28: */
29: /**
30: * Helper.java
31: *
32: * SUN PROPRIETARY/CONFIDENTIAL.
33: * This software is the proprietary information of Sun Microsystems, Inc.
34: * Use is subject to license terms.
35: *
36: * Created on November 16, 2004, 3:23 PM
37: */package com.sun.jbi.internal.security.https.jregress;
38:
39: import java.net.HttpURLConnection;
40: import java.io.BufferedWriter;
41: import java.io.File;
42: import java.io.InputStreamReader;
43: import java.io.InputStream;
44: import java.io.Reader;
45: import java.io.StringReader;
46: import java.io.Writer;
47:
48: /**
49: *
50: * @author Sun Microsystems, Inc.
51: */
52: public class Helper {
53:
54: /**
55: * Get the response and print it.
56: */
57: public static void getResponse(HttpURLConnection connection,
58: Writer writer) throws Exception {
59:
60: InputStream istr = null;
61: Reader reader = null;
62: try {
63: istr = connection.getInputStream();
64: reader = new InputStreamReader(istr);
65: } catch (java.io.IOException ioex) {
66: StringBuffer buf = new StringBuffer();
67: buf.append("HTTP Error Code: "
68: + connection.getResponseCode() + "\n");
69: buf.append("HTTP Error Message: "
70: + connection.getResponseMessage() + "\n");
71: reader = new StringReader(buf.toString().trim());
72: }
73:
74: if (reader != null) {
75: final int len = 256;
76: char cbuf[] = new char[len];
77:
78: int bytesRead = reader.read(cbuf, 0, len);
79:
80: BufferedWriter bufWriter = new BufferedWriter(writer);
81: while (bytesRead != -1) {
82: bufWriter.write(cbuf, 0, bytesRead);
83:
84: bytesRead = reader.read(cbuf, 0, len);
85:
86: }
87: ;
88: bufWriter.flush();
89: bufWriter.close();
90:
91: reader.close();
92: }
93: }
94:
95: }
|