001: /*
002: * CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF
003: * NETSCAPE COMMUNICATIONS CORPORATION
004: *
005: * Copyright (c) 1996 Netscape Communications Corporation.
006: * All Rights Reserved.
007: * Use of this Source Code is subject to the terms of the applicable
008: * license agreement from Netscape Communications Corporation.
009: */
010:
011: package communications;
012:
013: import util.ReportError;
014:
015: import java.io.DataOutputStream;
016: import java.io.OutputStream;
017: import java.io.DataInputStream;
018: import java.io.IOException;
019:
020: /**
021: * Wraps up managing a "post" style connection.
022: * <p>
023: * Note that the internal URLConnection,
024: * contrary to the URLConnection default, is
025: * <i>setDoOutput(true)</i>.
026: */
027: public class PostConnection extends ReadConnection {
028: /**
029: * The output stream.
030: */
031: protected OutputStream os;
032:
033: /**
034: * The data output stream.
035: */
036: protected DataOutputStream dos;
037:
038: /**
039: * Whether the current stream is valid.
040: */
041: protected boolean validOut;
042:
043: /**
044: * Constructs a PostConnection.
045: * @param s the URL
046: */
047: public PostConnection(String s) {
048: super (s);
049: os = null;
050: dos = null;
051: validOut = false;
052: }
053:
054: /**
055: * Sets the URL.
056: * @param s the URL
057: */
058: public void setURL(String s) {
059: super .setURL(s);
060: validOut = false;
061: }
062:
063: /**
064: * Opens the connection.
065: */
066: protected boolean openCon() {
067: if (validCon)
068: return true;
069: boolean b = super .openCon();
070: urlc.setDoOutput(true);
071: return b;
072: }
073:
074: /**
075: * Returns whether the stream valid.
076: */
077: public boolean isValidOut() {
078: return validOut;
079: }
080:
081: /**
082: * Opens the output stream.
083: */
084: public boolean openOut() {
085: if (validOut)
086: return true;
087:
088: openCon();
089:
090: try {
091: os = urlc.getOutputStream();
092: } catch (IOException e) {
093: ReportError.reportError(util.ReportError.INTERNAL,
094: "PostConnection", "openOut",
095: Messages.DATAOUTPUTSTREAMOPENFAIL);
096: return false;
097: }
098:
099: dos = new DataOutputStream(os);
100:
101: validOut = true;
102: return true;
103: }
104:
105: /**
106: * Closes the output stream.
107: */
108: public boolean closeOut() {
109: if (!validOut)
110: return true;
111:
112: validOut = false;
113:
114: try {
115: dos.close();
116: } catch (IOException e) {
117: ReportError.reportError(util.ReportError.INTERNAL,
118: "PostConnection", "closeOut",
119: Messages.DATAOUTPUTSTREAMCLOSEFAIL);
120: return false;
121: }
122:
123: return true;
124: }
125:
126: /**
127: * Write bytes to the data output stream.
128: * @param s bytes to write
129: */
130: public void writeBytes(String s) {
131: if (validOut) {
132: try {
133: dos.writeBytes(s);
134: } catch (IOException e) {
135: ReportError.reportError(util.ReportError.INTERNAL,
136: "PostConnection", "writeBytes",
137: Messages.DATAOUTPUTSTREAMWRITEFAIL);
138: }
139: }
140: }
141:
142: /**
143: * Encapsulated post action.
144: * @param s data to post
145: */
146: public String doWrite(String s) {
147: return doWrite(s, true);
148: }
149:
150: /**
151: * Encapsulated post action.
152: * @param s data to post
153: * @param b do read after post
154: */
155: public String doWrite(String s, boolean b) {
156: if (openOut() == false) {
157: return null;
158: }
159:
160: writeBytes(s);
161:
162: if (closeOut() == false) {
163: return null;
164: }
165:
166: if (b) {
167: String response = doRead();
168: if (response != null) {
169: response.trim();
170: }
171: return response;
172: }
173:
174: return null;
175: }
176:
177: /**
178: * Encapsulated post action.
179: * Assumes that a read should be attempted after the post.
180: * @param url target url
181: * @param s data to post
182: */
183: public static String doWrite(String url, String s) {
184: return doWrite(url, s, true);
185: }
186:
187: /**
188: * Encapsulated post action.
189: * @param url target url
190: * @param s data to post
191: * @param b do a get after the post
192: */
193: public static String doWrite(String url, String s, boolean b) {
194: PostConnection pc = new PostConnection(url);
195:
196: if (pc.openOut() == false) {
197: return null;
198: }
199:
200: pc.writeBytes(s);
201:
202: if (pc.closeOut() == false) {
203: return null;
204: }
205:
206: if (b) {
207: String response = pc.doRead();
208: if (response != null) {
209: response.trim();
210: }
211: return response;
212: }
213:
214: return null;
215: }
216:
217: }
|