Skip to content

Commit

Permalink
Added open(HttpTransfer) method
Browse files Browse the repository at this point in the history
  • Loading branch information
igr committed Dec 22, 2013
1 parent dfd3b6b commit 51a3827
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 16 deletions.
2 changes: 2 additions & 0 deletions jodd-http/src/main/java/jodd/http/HttpBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public void sendRequest(HttpRequest httpRequest) {
}
}

// ---------------------------------------------------------------- cookies

/**
* Reads cookies from response.
*/
Expand Down
19 changes: 14 additions & 5 deletions jodd-http/src/main/java/jodd/http/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,11 @@ public HttpRequest setHostHeader() {
protected HttpTransport httpTransport;

/**
* Opens transport i.e. connection. Returns used {@link HttpTransport} implementation.
* Opens transport using provided implementation
* of {@link jodd.http.HttpTransport}.
*/
public HttpTransport open() {
httpTransport = new HttpTransport();
public HttpTransport open(HttpTransport httpTransport) {
this.httpTransport = httpTransport;

try {
httpTransport.open(this);
Expand All @@ -438,7 +439,15 @@ public HttpTransport open() {
}

/**
* Opens request if not already open, sends request, reads response and closes the request.
* Opens transport i.e. connection. Returns used {@link HttpTransport} implementation.
*/
public HttpTransport open() {
return this.open(new HttpTransport());
}

/**
* {@link #open() Opens request} if not already open, sends request,
* reads response and closes the request.
*/
public HttpResponse send() {
if (httpTransport == null) {
Expand All @@ -447,7 +456,7 @@ public HttpResponse send() {

HttpResponse httpResponse;
try {
httpResponse = httpTransport.send();
httpResponse = httpTransport.send(this);
} catch (IOException ioex) {
throw new HttpException(ioex);
}
Expand Down
25 changes: 15 additions & 10 deletions jodd-http/src/main/java/jodd/http/HttpTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,44 @@
import java.net.Socket;

/**
* Socket adapter for {@link HttpRequest}.
* Socket adapter used by {@link HttpRequest}.
*/
public class HttpTransport {

protected Socket socket;
protected HttpRequest httpRequest;

/**
* Creates new socket from current host and port.
* Creates new socket from current {@link jodd.http.HttpRequest request}.
* @see #createSocket(javax.net.SocketFactory, String, int)
*/
public Socket open(HttpRequest httpRequest) throws IOException {
this.httpRequest = httpRequest;

if (httpRequest.protocol().equals("https")) {
SocketFactory ssocketFactory = SSLSocketFactory.getDefault();
SSLSocket sslSocket = (SSLSocket) ssocketFactory.createSocket(httpRequest.host(), 443);
SSLSocket sslSocket = (SSLSocket) createSocket(
SSLSocketFactory.getDefault(), httpRequest.host(), 443);
sslSocket.startHandshake();

this.socket = sslSocket;
}
else {
this.socket = new Socket(httpRequest.host(), httpRequest.port());
this.socket = createSocket(
SocketFactory.getDefault(), httpRequest.host(), httpRequest.port());
}

return socket;
}

/**
* Creates a socket with provided socket factory.
*/
protected Socket createSocket(SocketFactory socketFactory, String host, int port) throws IOException {
return socketFactory.createSocket(host, port);
}

/**
* Opens sockets output stream and sends request data to it.
* Returns parsed response.
*/
public HttpResponse send() throws IOException {

public HttpResponse send(HttpRequest httpRequest) throws IOException {
OutputStream outputStream = socket.getOutputStream();

httpRequest.sendTo(outputStream);
Expand Down
45 changes: 45 additions & 0 deletions jodd-http/src/test/java/jodd/http/HttpBrowserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2003-2013, Jodd Team (jodd.org). All Rights Reserved.

package jodd.http;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class HttpBrowserTest {

static TestServer testServer;

@BeforeClass
public static void startServer() throws Exception {
testServer = new TomcatServer();
testServer.start();
}

@AfterClass
public static void stopServer() throws Exception {
testServer.stop();
}

@Before
public void setUp() {
EchoServlet.testinit();
}

@Test
public void testBrowser() {
HttpBrowser httpBrowser = new HttpBrowser();

httpBrowser.sendRequest(
HttpRequest.get("localhost:8080/echo?id=17").bodyText("hello"));
HttpResponse httpResponse = httpBrowser.getHttpResponse();

assertNotNull(httpResponse);
assertEquals("hello", httpResponse.body());
}

}
2 changes: 1 addition & 1 deletion jodd-http/src/test/java/jodd/http/TomcatServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.apache.catalina.startup.Tomcat;

/**
* Madvoc Tomcat Server.
* Tomcat Server.
*/
public class TomcatServer extends TestServer {

Expand Down

0 comments on commit 51a3827

Please sign in to comment.