001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb;
032:
033: import java.io.IOException;
034:
035: import org.hsqldb.lib.InOutUtil;
036:
037: /**
038: * HTTP protocol session proxy implementation. Uses the updated HSQLDB HTTP
039: * sub protocol.
040: *
041: * @author fredt@users
042: * @version 1.7.2
043: * @since 1.7.2
044: */
045: public class HTTPClientConnection extends HSQLClientConnection {
046:
047: static final String ENCODING = "8859_1";
048:
049: public HTTPClientConnection(String host, int port, String path,
050: String database, boolean isTLS, String user, String password)
051: throws HsqlException {
052: super (host, port, path, database, isTLS, user, password);
053: }
054:
055: protected void initConnection(String host, int port, boolean isTLS)
056: throws HsqlException {
057: }
058:
059: public synchronized Result execute(Result r) throws HsqlException {
060:
061: super .openConnection(host, port, isTLS);
062:
063: Result result = super .execute(r);
064:
065: super .closeConnection();
066:
067: return result;
068: }
069:
070: protected void write(Result r) throws IOException, HsqlException {
071:
072: rowOut.reset();
073: r.write(rowOut);
074: dataOutput.write("POST ".getBytes(ENCODING));
075: dataOutput.write(path.getBytes(ENCODING));
076: dataOutput.write(" HTTP/1.0\r\n".getBytes(ENCODING));
077: dataOutput.write("Content-Type: application/octet-stream\r\n"
078: .getBytes(ENCODING));
079: dataOutput.write(("Content-Length: " + rowOut.size() + "\r\n")
080: .getBytes(ENCODING));
081: dataOutput.write("\r\n".getBytes(ENCODING));
082: dataOutput.write(rowOut.getOutputStream().getBuffer(), 0,
083: rowOut.getOutputStream().size());
084: dataOutput.flush();
085: }
086:
087: protected Result read() throws IOException, HsqlException {
088:
089: // fredt - for WebServer 4 lines should be skipped
090: // for Servlet, number of lines depends on Servlet container
091: // stop skipping after the blank line
092: rowOut.reset();
093:
094: for (;;) {
095: int count = InOutUtil.readLine(dataInput, rowOut);
096:
097: if (count <= 2) {
098: break;
099: }
100: }
101:
102: //
103: Result resultIn = Result.read(rowIn, dataInput);
104:
105: return resultIn;
106: }
107: }
|