This commit is contained in:
Georg Richter 2015-01-30 14:19:04 +01:00
Родитель 6e4a1bbbd2
Коммит c52acdb7f5
9 изменённых файлов: 12 добавлений и 599 удалений

Просмотреть файл

@ -1,3 +0,0 @@
.classpath
.project
.settings

6
.gitattributes поставляемый Normal file
Просмотреть файл

@ -0,0 +1,6 @@
# Normalise line endings:
* text=auto
# Prevent certain files from being exported:
.gitattributes export-ignore
.gitignore export-ignore

6
.gitignore поставляемый Normal file
Просмотреть файл

@ -0,0 +1,6 @@
.classpath
.project
.settings
# Keep empty directories:
# Keep empty directories: >> .gitignore/.git*

Просмотреть файл

@ -1,255 +0,0 @@
/*
* Drizzle-JDBC
*
* Copyright (c) 2009-2011, Marcus Eriksson
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
* conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the driver nor the names of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.skysql.jdbc;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DrizzleDataBaseMetaData extends CommonDatabaseMetaData {
public DrizzleDataBaseMetaData(Builder builder) {
super(builder);
}
/**
* Retrieves a description of the given table's primary key columns. They are ordered by COLUMN_NAME.
* <p/>
* <P>Each primary key column description has the following columns: <OL> <LI><B>TABLE_CAT</B> String => table
* catalog (may be <code>null</code>) <LI><B>TABLE_SCHEM</B> String => table schema (may be <code>null</code>)
* <LI><B>TABLE_NAME</B> String => table name <LI><B>COLUMN_NAME</B> String => column name <LI><B>KEY_SEQ</B> short
* => sequence number within primary key( a value of 1 represents the first column of the primary key, a value of 2
* would represent the second column within the primary key). <LI><B>PK_NAME</B> String => primary key name (may be
* <code>null</code>) </OL>
*
* @param catalog a catalog name; must match the catalog name as it is stored in the database; "" retrieves those
* without a catalog; <code>null</code> means that the catalog name should not be used to narrow the
* search
* @param schema a schema name; must match the schema name as it is stored in the database; "" retrieves those
* without a schema; <code>null</code> means that the schema name should not be used to narrow the
* search
* @param table a table name; must match the table name as it is stored in the database
* @return <code>ResultSet</code> - each row is a primary key column description
* @throws java.sql.SQLException if a database access error occurs
*/
@Override
public ResultSet getPrimaryKeys(final String catalog, final String schema, final String table) throws SQLException {
String query = "SELECT null TABLE_CAT, " +
"columns.table_schema TABLE_SCHEM, " +
"columns.table_name, " +
"columns.column_name, " +
"kcu.ordinal_position KEY_SEQ," +
"null pk_name " +
"FROM information_schema.columns " +
"INNER JOIN information_schema.key_column_usage kcu "+
"ON kcu.constraint_schema = columns.table_schema AND " +
"columns.table_name = kcu.table_name AND " +
"columns.column_name = kcu.column_name " +
"WHERE columns.table_name='" + table + "' AND kcu.constraint_name='PRIMARY'";
if (schema != null) {
query += " AND columns.table_schema = '" + schema + "'";
}
query += " ORDER BY columns.column_name";
final Statement stmt = getConnection().createStatement();
return stmt.executeQuery(query);
}
/**
* Maps standard table types to mysql ones - helper since table type is never "table" in mysql, it is "base table"
* @param tableType the table type defined by user
* @return the internal table type.
*/
private String mapTableTypes(String tableType) {
if(tableType.equals("TABLE")) {
return "BASE TABLE";
}
if(tableType.equals("SYSTEM VIEW")) {
return "VIEW";
}
return tableType;
}
@Override
public ResultSet getTables(final String catalog, final String schemaPattern, final String tableNamePattern, final String[] types) throws SQLException {
String query = "SELECT table_catalog table_cat, "
+ "table_schema table_schem, "
+ "table_name, "
+ "table_type, "
+ "'remarks' as remarks,"
+ "null as type_cat, "
+ "null as type_schem,"
+ "null as type_name, "
+ "null as self_referencing_col_name,"
+ "null as ref_generation "
+ "FROM information_schema.tables "
+ "WHERE table_name LIKE \""+(tableNamePattern == null?"%":tableNamePattern)+"\""
+ getSchemaPattern(schemaPattern);
if(types != null) {
query += " AND table_type in (";
boolean first = true;
for(String s : types) {
String mappedType = mapTableTypes(s);
if(!first) {
query += ",";
}
first = false;
query += "'"+mappedType+"'";
}
query += ")";
}
final Statement stmt = getConnection().createStatement();
return stmt.executeQuery(query);
}
public ResultSet getColumns(final String catalog, final String schemaPattern, final String tableNamePattern, final String columnNamePattern)
throws SQLException {
final String query = " SELECT null as table_cat," +
" table_schema as table_schem," +
" table_name," +
" column_name," +
dataTypeClause + " data_type," +
" data_type type_name," +
" character_maximum_length column_size," +
" 0 buffer_length," +
" numeric_precision decimal_digits," +
" numeric_scale num_prec_radix," +
" if(is_nullable='yes',1,0) nullable," +
" 'remarks' remarks," +
" column_default column_def," +
" 0 sql_data," +
" 0 sql_datetime_sub," +
" character_octet_length char_octet_length," +
" ordinal_position," +
" is_nullable," +
" null scope_catalog," +
" null scope_schema," +
" null scope_table," +
" null source_data_type," +
" '' is_autoincrement" +
" FROM information_schema.columns " +
"WHERE table_schema LIKE '" + ((schemaPattern == null) ? "%" : schemaPattern) + "'" +
" AND table_name LIKE '" + ((tableNamePattern == null) ? "%" : tableNamePattern) + "'" +
" AND column_name LIKE '" + ((columnNamePattern == null) ? "%" : columnNamePattern) + "'" +
" ORDER BY table_cat, table_schem, table_name, ordinal_position";
final Statement stmt = getConnection().createStatement();
return stmt.executeQuery(query);
}
public ResultSet getExportedKeys(final String catalog, final String schema, final String table) throws SQLException {
String query = "SELECT null PKTABLE_CAT,\n" +
" fk.constraint_schema PKTABLE_SCHEM,\n" +
" fk.referenced_table_name PKTABLE_NAME,\n" +
" replace(fk.referenced_table_columns,'`','') PKCOLUMN_NAME,\n" +
" null FKTABLE_CAT,\n" +
" fk.constraint_schema FKTABLE_SCHEM,\n" +
" fk.constraint_table FKTABLE_NAME,\n" +
" replace(fk.constraint_columns,'`','') FKCOLUMN_NAME,\n" +
" 1 KEY_SEQ,\n" +
" CASE update_rule\n" +
" WHEN 'RESTRICT' THEN 1\n" +
" WHEN 'NO ACTION' THEN 3\n" +
" WHEN 'CASCADE' THEN 0\n" +
" WHEN 'SET NULL' THEN 2\n" +
" WHEN 'SET DEFAULT' THEN 4\n" +
" END UPDATE_RULE,\n" +
" CASE delete_rule\n" +
" WHEN 'RESTRICT' THEN 1\n" +
" WHEN 'NO ACTION' THEN 3\n" +
" WHEN 'CASCADE' THEN 0\n" +
" WHEN 'SET NULL' THEN 2\n" +
" WHEN 'SET DEFAULT' THEN 4\n" +
" END UPDATE_RULE,\n" +
" fk.constraint_name FK_NAME,\n" +
" null PK_NAME,\n" +
" 6 DEFERRABILITY\n" +
"FROM data_dictionary.foreign_keys fk "+
"WHERE " +
(schema != null ? "fk.constraint_schema='" + schema + "' AND " : "") +
"fk.referenced_table_name='" +
table +
"' " +
"ORDER BY FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ";
final Statement stmt = getConnection().createStatement();
return stmt.executeQuery(query);
}
public ResultSet getImportedKeys(final String catalog, final String schema, final String table) throws SQLException {
final String query = "SELECT null PKTABLE_CAT,\n" +
"fk.constraint_schema PKTABLE_SCHEM,\n" +
"fk.referenced_table_name PKTABLE_NAME,\n" +
"replace(fk.referenced_table_columns,'`','') PKCOLUMN_NAME,\n" +
"null FKTABLE_CAT,\n" +
"fk.constraint_schema FKTABLE_SCHEM,\n" +
"fk.constraint_table FKTABLE_NAME,\n" +
"replace(fk.constraint_columns,'`','') FKCOLUMN_NAME,\n" +
"1 KEY_SEQ,\n" +
"CASE update_rule\n" +
" WHEN 'RESTRICT' THEN 1\n" +
" WHEN 'NO ACTION' THEN 3\n" +
" WHEN 'CASCADE' THEN 0\n" +
" WHEN 'SET NULL' THEN 2\n" +
" WHEN 'SET DEFAULT' THEN 4\n" +
"END UPDATE_RULE,\n" +
"CASE delete_rule\n" +
" WHEN 'RESTRICT' THEN 1\n" +
" WHEN 'NO ACTION' THEN 3\n" +
" WHEN 'CASCADE' THEN 0\n" +
" WHEN 'SET NULL' THEN 2\n" +
" WHEN 'SET DEFAULT' THEN 4\n" +
"END UPDATE_RULE,\n" +
"fk.constraint_name FK_NAME,\n" +
"null PK_NAME,\n" +
"6 DEFERRABILITY\n" +
"FROM data_dictionary.foreign_keys fk "+
"WHERE " +
(schema != null ? "fk.constraint_schema='" + schema + "' AND " : "") +
"fk.constraint_table='" +
table +
"'" +
"ORDER BY FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ";
final Statement stmt = getConnection().createStatement();
return stmt.executeQuery(query);
}
public ResultSet getBestRowIdentifier(final String catalog, final String schema, final String table, final int scope, final boolean nullable)
throws SQLException {
final String query = "SELECT " + DatabaseMetaData.bestRowSession + " scope," +
"column_name," +
dataTypeClause + " data_type," +
"data_type type_name," +
"if(numeric_precision is null, character_maximum_length, numeric_precision) column_size," +
"0 buffer_length," +
"numeric_scale decimal_digits," +
DatabaseMetaData.bestRowNotPseudo + " pseudo_column" +
" FROM data_dictionary.columns" +
" WHERE is_indexed = 'YES' OR is_used_in_primary = 'YES' OR is_unique = 'YES'" +
" AND table_schema like " + (schema != null ? "'%'" : "'" + schema + "'") +
" AND table_name='" + table + "' ORDER BY scope";
final Statement stmt = getConnection().createStatement();
return stmt.executeQuery(query);
}
}

Просмотреть файл

@ -1,51 +0,0 @@
/*
* Drizzle-JDBC
*
* Copyright (c) 2009-2011, Marcus Eriksson
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
* conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the driver nor the names of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.skysql.jdbc.internal.common;
import java.util.regex.Pattern;
/**
* Created by IntelliJ IDEA. User: marcuse Date: Jun 13, 2009 Time: 12:49:55 PM To change this template use File |
* Settings | File Templates.
*/
public enum SupportedDatabases {
MYSQL("MySQL"), DRIZZLE("Drizzle");
private final String databaseName;
private static final Pattern drizzlePattern = Pattern.compile("^201\\d\\..*"); //will work for 9 years atleast!
SupportedDatabases(final String databaseName) {
this.databaseName = databaseName;
}
public String getDatabaseName() {
return databaseName;
}
public static SupportedDatabases fromVersionString(String version) {
if(drizzlePattern.matcher(version).matches())
return SupportedDatabases.DRIZZLE;
return SupportedDatabases.MYSQL;
}
}

Просмотреть файл

@ -1,90 +0,0 @@
/*
* Drizzle-JDBC
*
* Copyright (c) 2009-2011, Marcus Eriksson
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
* conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the driver nor the names of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.skysql.jdbc.internal.drizzle;
import org.skysql.jdbc.MySQLBlob;
import org.skysql.jdbc.internal.common.AbstractValueObject;
import org.skysql.jdbc.internal.common.DataType;
import java.text.ParseException;
/**
* Contains the raw value returned from the server
* <p/>
* Is immutable
* <p/>
* User: marcuse Date: Feb 16, 2009 Time: 9:18:26 PM
*/
public class DrizzleValueObject extends AbstractValueObject {
public DrizzleValueObject(final byte[] rawBytes, final DataType dataType) {
super(rawBytes, dataType);
}
public Object getObject(int datatypeMappingFlags) throws ParseException {
if (this.getBytes() == null) {
return null;
}
switch (dataType.getType()) {
case TINYINT:
return getBoolean();
case INTEGER:
return getLong();
case DOUBLE:
return getDouble();
case TIMESTAMP:
return getTimestamp();
case BIGINT:
return getBigInteger();
case DATETIME:
return getTimestamp();
case DATE:
return getDate();
case VARCHAR:
return getString();
case DECIMAL:
return getBigDecimal();
case BLOB:
return new MySQLBlob(getBytes());
case YEAR:
return getString();
case BIT:
if(getBytes().length == 1) {
return getBytes()[0] == 1;
}
return null;
case SMALLINT:
case MEDIUMINT:
return getInt();
case FLOAT:
return getFloat();
case TIME:
return getTime();
case CHAR:
return getString();
}
return null;
}
}

Просмотреть файл

@ -1,57 +0,0 @@
/*
* Drizzle-JDBC
*
* Copyright (c) 2009-2011, Marcus Eriksson
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
* conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the driver nor the names of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.skysql.jdbc.internal.drizzle.packet;
import org.skysql.jdbc.internal.common.ColumnInformation;
import org.skysql.jdbc.internal.common.ValueObject;
import org.skysql.jdbc.internal.common.packet.RawPacket;
import org.skysql.jdbc.internal.common.packet.buffer.Reader;
import org.skysql.jdbc.internal.drizzle.DrizzleValueObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* User: marcuse Date: Jan 23, 2009 Time: 9:28:43 PM
*/
public class DrizzleRowPacket {
private final List<ValueObject> columns;
public DrizzleRowPacket(final RawPacket rawPacket, final List<ColumnInformation> columnInformation) throws IOException {
columns = new ArrayList<ValueObject>(columnInformation.size());
final Reader reader = new Reader(rawPacket);
for (final ColumnInformation currentColumn : columnInformation) {
final ValueObject dvo = new DrizzleValueObject(reader.getLengthEncodedBytes(), currentColumn.getType());
columns.add(dvo);
currentColumn.updateDisplaySize(dvo.getDisplayLength());
}
}
public List<ValueObject> getRow() {
return columns;
}
}

Просмотреть файл

@ -1,55 +0,0 @@
package org.skysql.jdbc;
import org.junit.Ignore;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import static junit.framework.Assert.assertEquals;
/**
* User: marcuse
* Date: Jan 14, 2009
* Time: 7:58:11 AM
*/
@Ignore
public class BlobStreamingTest {
public static String host = "localhost";
private Connection connection;
static { Logger.getLogger("").setLevel(Level.OFF); }
public BlobStreamingTest() throws SQLException {
//connection = DriverManager.getConnection("jdbc:mysql:thin://localhost:3306/test");
connection = DriverManager.getConnection("jdbc:drizzle://"+host+":3307/test?enableBlobStreaming=true");
//connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test");
}
// @After
public void close() throws SQLException {
connection.close();
}
public Connection getConnection() {
return connection;
}
// @Test
public void doQuery() throws SQLException, IOException {
Statement stmt = getConnection().createStatement();
stmt.execute("drop table if exists bstreaming1");
stmt.execute("create table bstreaming1 (id int not null primary key auto_increment, test longblob)");
PreparedStatement ps = getConnection().prepareStatement("insert into bstreaming1 values (null, ?)");
ByteArrayInputStream bais = new ByteArrayInputStream("HEJHEJHEJ".getBytes());
ps.setBinaryStream(1, bais);
ps.executeUpdate();
stmt = getConnection().createStatement();
ResultSet rs = stmt.executeQuery("select * from bstreaming1");
assertEquals(rs.next(), true);
byte[] b = new byte[100];
int l = rs.getBinaryStream("test").read(b);
assertEquals("HEJHEJHEJ",new String(b,0,l));
}
}

Просмотреть файл

@ -1,88 +0,0 @@
package org.skysql.jdbc;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import org.skysql.jdbc.internal.common.HttpClient;
import org.junit.Ignore;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import static org.junit.Assert.assertEquals;
/**
* Created by IntelliJ IDEA. User: marcuse Date: Aug 9, 2010 Time: 9:04:39 PM To change this template use File |
* Settings | File Templates.
*/
@Ignore
public class HttpClientTest {
//@Test
public void testGet() throws IOException {
HttpHandler handler = new HttpHandler() {
public void handle(HttpExchange httpExchange) throws IOException {
if (httpExchange.getRequestMethod().equalsIgnoreCase("GET")) {
Headers responseHeaders = httpExchange.getResponseHeaders();
responseHeaders.set("Content-Type", "text/plain");
httpExchange.sendResponseHeaders(200, 0);
OutputStream responseBody = httpExchange.getResponseBody();
responseBody.write("hhhhh".getBytes());
responseBody.close();
}
}
};
InetSocketAddress isa = new InetSocketAddress(9998);
HttpServer server = HttpServer.create(isa,0);
server.createContext("/apa", handler);
server.setExecutor(Executors.newCachedThreadPool());
server.start();
HttpClient httpClient = new HttpClient("http://localhost:9998/apa");
InputStream is = httpClient.get();
byte[] b = new byte[is.available()];
is.read(b);
assertEquals("hhhhh", new String(b));
server.stop(0);
}
// @Test
public void testPut() throws IOException {
HttpHandler handler = new HttpHandler() {
public void handle(HttpExchange httpExchange) throws IOException {
if (httpExchange.getRequestMethod().equalsIgnoreCase("PUT")) {
InputStream is = httpExchange.getRequestBody();
byte [] buf = new byte[1000];
int len = is.read(buf);
String s = new String(buf, 0, len);
Headers responseHeaders = httpExchange.getResponseHeaders();
responseHeaders.set("Content-Type", "text/plain");
httpExchange.sendResponseHeaders(200, 0);
OutputStream responseBody = httpExchange.getResponseBody();
responseBody.write(s.toLowerCase().getBytes());
responseBody.close();
}
}
};
InetSocketAddress isa = new InetSocketAddress(9999);
HttpServer server = HttpServer.create(isa,0);
server.createContext("/apa", handler);
server.setExecutor(Executors.newCachedThreadPool());
server.start();
HttpClient httpClient = new HttpClient("http://localhost:9999/apa");
ByteArrayInputStream bais = new ByteArrayInputStream("HEJ HEJ".getBytes());
String ret = httpClient.put(bais);
assertEquals("hej hej", ret);
server.stop(0);
}
}