Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,27 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TestServlet extends HttpServlet {

private static final long serialVersionUID = 8775172147016982644L;
private static final Logger LOGGER = Logger.getLogger(TestServlet.class.getName());

private static final String REQUEST_RESPONSE_FAILURE_MSG_BASE = "The request / response interaction generated an exception";

@Override protected void doPut(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().append(request.getReader().readLine());
try {
response.getWriter().append(request.getReader().readLine());
} catch (IOException ex) {
LOGGER.log(Level.FINE, REQUEST_RESPONSE_FAILURE_MSG_BASE, ex);
}
}

@Override protected void doPost(
Expand All @@ -36,6 +48,11 @@ public class TestServlet extends HttpServlet {
throws ServletException, IOException {
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().append(request.getReader().readLine());
try {
response.getWriter().append(request.getReader().readLine());
} catch (IOException ex) {
LOGGER.log(Level.FINE, REQUEST_RESPONSE_FAILURE_MSG_BASE, ex);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.thoughtworks.inproctester.jetty.testapp.validation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

public class ValidatingHttpRequest extends HttpServletRequestWrapper {

public ValidatingHttpRequest(HttpServletRequest request) {
super(request);
}

@Override
public boolean authenticate(HttpServletResponse response) {
// Customize this
return true;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.thoughtworks.inproctester.jetty.testapp.validation;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;



public class ValidationFilter implements javax.servlet.Filter {

@Override
public void destroy() {
// Customise this
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(new ValidatingHttpRequest((HttpServletRequest) request), response);
}

@Override
public void init(FilterConfig arg0) throws ServletException {
// Customize
}
}

32 changes: 22 additions & 10 deletions inproctester-jetty-tests/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,26 @@
Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>
com.thoughtworks.inproctester.jetty.testapp.TestServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>
com.thoughtworks.inproctester.jetty.testapp.TestServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

<filter>
<filter-name>ValidationFilter</filter-name>
<filter-class>com.thoughtworks.inproctester.jetty.testapp.validation.ValidationFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>ValidationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


</web-app>
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
@Path("/")
public class TestApplication {

private static Map<Integer, TestResource> resources = new HashMap<Integer, TestResource>();
private static Map<Integer, TestResource> resources = new HashMap<>();

@Context
private UriInfo uriInfo;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.thoughtworks.inproctester.resteasy.testapp.validation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

public class ValidatingHttpRequest extends HttpServletRequestWrapper {

public ValidatingHttpRequest(HttpServletRequest request) {
super(request);
}

@Override
public boolean authenticate(HttpServletResponse response) {
// Customize this
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.thoughtworks.inproctester.resteasy.testapp.validation;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class ValidationFilter implements javax.servlet.Filter {

@Override
public void destroy() {
// Customise this
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(new ValidatingHttpRequest((HttpServletRequest) request), response);
}

@Override
public void init(FilterConfig arg0) throws ServletException {
// Customize
}
}
9 changes: 9 additions & 0 deletions inproctester-resteasy-tests/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@ Application 2.3//EN"
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>ValidationFilter</filter-name>
<filter-class>com.thoughtworks.inproctester.resteasy.testapp.validation.ValidationFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>ValidationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class InProcessClientExecutor implements ClientExecutor {


private static final Logger LOGGER = Logger.getLogger(InProcessClientExecutor.class.getName());
private static final String CONNECTION_RELEASE_FAILURE_MSG = "An exception occured while trying to release the connection";

private List<TesterRoute> testerRoutes = new ArrayList<>();

public InProcessClientExecutor() {
Expand Down Expand Up @@ -63,7 +68,8 @@ public InputStream getInputStream() {
public void performReleaseConnection() {
try {
stream.close();
} catch (Exception ignored) {
} catch (Exception ex) {
LOGGER.log(Level.FINE,CONNECTION_RELEASE_FAILURE_MSG , ex);
}
}
}, this);
Expand All @@ -75,6 +81,7 @@ public void performReleaseConnection() {
}

public void close() throws Exception {
// Not implemented yet
}

private InProcConnection routeToTesterApplication(URI requestUri) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import com.thoughtworks.inproctester.core.InProcRequest;
import com.thoughtworks.inproctester.core.UrlHelper;
import com.thoughtworks.inproctester.resteasy.exceptions.RequestEntityWriteException;
import com.thoughtworks.inproctester.resteasy.exceptions.RequestHostException;
import com.thoughtworks.inproctester.resteasy.exceptions.UriRetrievalException;

import org.jboss.resteasy.client.ClientRequest;

import javax.ws.rs.core.MultivaluedMap;
Expand All @@ -13,17 +17,23 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RestEasyClientInProcRequest implements InProcRequest {

private static final Logger LOGGER = Logger.getLogger(RestEasyClientInProcRequest.class.getName());
private static final String REQUEST_ENTITY_WRITE_EXCEPTION_MESSAGE = "Unable to write from request entity";

private ClientRequest clientRequest;
private Map<String, String> headers = new HashMap<String, String>();
private Map<String, String> headers = new HashMap<>();

public RestEasyClientInProcRequest(ClientRequest clientRequest) {
this.clientRequest = clientRequest;
try {
headers.put("Host", UrlHelper.getRequestHost(new URI(clientRequest.getUri())));
} catch (Exception e) {
throw new RuntimeException(e);
throw new RequestHostException(e);
}
if (clientRequest.getBodyContentType() != null) {
headers.put("Content-type", clientRequest.getBodyContentType().toString());
Expand All @@ -41,7 +51,7 @@ public URI getUri() {
try {
return new URI(clientRequest.getUri());
} catch (Exception e) {
throw new RuntimeException(e);
throw new UriRetrievalException(e);
}
}

Expand All @@ -50,7 +60,7 @@ public String getContent() {
try {
return new String(writeRequestEntity(clientRequest), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
throw new RequestEntityWriteException(e);
}
}

Expand All @@ -70,7 +80,7 @@ public void addHeader(String headerName, String header) {
}

private Map<String, String> asMap(MultivaluedMap<String, String> headers) {
HashMap<String, String> map = new HashMap<String, String>();
HashMap<String, String> map = new HashMap<>();
for (Map.Entry<String, List<String>> header : headers.entrySet()) {
for (String v : header.getValue()) {
map.put(header.getKey(), v);
Expand All @@ -83,7 +93,7 @@ private Map<String, String> asMap(MultivaluedMap<String, String> headers) {
private byte[] writeRequestEntity(ClientRequest clientRequest) {

if (clientRequest.getBody() != null && !clientRequest.getFormParameters().isEmpty())
throw new RuntimeException("You cannot send both form parameters and an entity body");
throw new RequestEntityWriteException("You cannot send both form parameters and an entity body");

if (!clientRequest.getFormParameters().isEmpty()) {
throw new UnsupportedOperationException("InProcessClientExecutpr doesn't support form parameters yet");
Expand All @@ -92,12 +102,13 @@ private byte[] writeRequestEntity(ClientRequest clientRequest) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (clientRequest.getBody() != null) {
if ("GET".equals(clientRequest.getHttpMethod()))
throw new RuntimeException("A GET request cannot have a body.");
throw new RequestEntityWriteException("A GET request cannot have a body.");

try {
clientRequest.writeRequestBody(clientRequest.getHeadersAsObjects(), baos);
} catch (IOException e) {
throw new RuntimeException(e);
LOGGER.log(Level.FINE, REQUEST_ENTITY_WRITE_EXCEPTION_MESSAGE, e);
throw new RequestEntityWriteException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.thoughtworks.inproctester.resteasy.exceptions;

public class RequestEntityWriteException extends RuntimeException {

private static final long serialVersionUID = -3024331508265244769L;

public RequestEntityWriteException(Exception e) {
super(e);
}

public RequestEntityWriteException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.thoughtworks.inproctester.resteasy.exceptions;

public class RequestHostException extends RuntimeException {

private static final long serialVersionUID = -1365627301588525745L;

public RequestHostException(Exception ex) {
super(ex);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.thoughtworks.inproctester.resteasy.exceptions;

public class UriRetrievalException extends RuntimeException {

private static final long serialVersionUID = -506077171726232405L;

public UriRetrievalException(Exception ex) {
super(ex);
}

}
Loading