Skip to content

Commit

Permalink
Updated the way URL twisting works. Before, it only worked with Servi…
Browse files Browse the repository at this point in the history
…ces in

Proxy mode, but now works with Static, Dynamic, and Proxy.
  • Loading branch information
clafonta committed May 1, 2013
1 parent b8411ee commit 16734ab
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 184 deletions.
11 changes: 11 additions & 0 deletions RELEASENOTES.txt
@@ -1,4 +1,15 @@


============================
May 1st 2013. Version: 2.0.2
============================

-----------------------------------------------------------------------------
CHANGES:
-----------------------------------------------------------------------------
Updated the way URL twisting works. Before, it only worked with Services in
Proxy mode, but now works with Static, Dynamic, and Proxy.

============================
April 2013. Version: 2.0.0
============================
Expand Down
4 changes: 2 additions & 2 deletions build.num
@@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit!
#Wed Apr 24 20:33:44 PDT 2013
build.number=1
#Wed May 01 09:06:39 PDT 2013
build.number=2
58 changes: 16 additions & 42 deletions src/java/com/mockey/ClientExecuteProxy.java
Expand Up @@ -56,7 +56,6 @@
import com.mockey.model.ProxyServerModel;
import com.mockey.model.RequestFromClient;
import com.mockey.model.ResponseFromService;
import com.mockey.model.TwistInfo;
import com.mockey.model.Url;

/**
Expand Down Expand Up @@ -99,48 +98,40 @@ private ClientExecuteProxy() {
* @return
* @throws ClientExecuteProxyException
*/
public ResponseFromService execute(TwistInfo twistInfo,
ProxyServerModel proxyServer, Url realServiceUrl,
boolean allowRedirectFollow, RequestFromClient request)
throws ClientExecuteProxyException {
public ResponseFromService execute(ProxyServerModel proxyServer, Url realServiceUrl, boolean allowRedirectFollow,
RequestFromClient request) throws ClientExecuteProxyException {
log.info("Request: " + String.valueOf(realServiceUrl));

// general setup
SchemeRegistry supportedSchemes = new SchemeRegistry();

// Register the "http" and "https" protocol schemes, they are
// required by the default operator to look up socket factories.
supportedSchemes.register(new Scheme("http", 80, PlainSocketFactory
.getSocketFactory()));
supportedSchemes.register(new Scheme("https", 443, SSLSocketFactory
.getSocketFactory()));
supportedSchemes.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
supportedSchemes.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));

// prepare parameters
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.ISO_8859_1);
HttpProtocolParams.setUseExpectContinue(params, false);

ClientConnectionManager ccm = new ThreadSafeClientConnManager(
supportedSchemes);
ClientConnectionManager ccm = new ThreadSafeClientConnManager(supportedSchemes);
DefaultHttpClient httpclient = new DefaultHttpClient(ccm, params);

if (!allowRedirectFollow) {
// Do NOT allow for 302 REDIRECT
httpclient.setRedirectStrategy(new DefaultRedirectStrategy() {
public boolean isRedirected(HttpRequest request,
HttpResponse response, HttpContext context) {
public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
boolean isRedirect = false;
try {
isRedirect = super.isRedirected(request, response,
context);
isRedirect = super.isRedirected(request, response, context);
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (!isRedirect) {
int responseCode = response.getStatusLine()
.getStatusCode();
int responseCode = response.getStatusLine().getStatusCode();
if (responseCode == 301 || responseCode == 302) {
return true;
}
Expand Down Expand Up @@ -177,42 +168,25 @@ public boolean isRedirected(HttpRequest request,

if (proxyServer.isProxyEnabled()) {
// make sure to use a proxy that supports CONNECT
httpclient.getCredentialsProvider().setCredentials(
proxyServer.getAuthScope(), proxyServer.getCredentials());
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
proxyServer.getHttpHost());
}

// TWISTING
Url originalRequestUrlBeforeTwisting = null;
if (twistInfo != null) {
String fullurl = realServiceUrl.getFullUrl();
String twistedUrl = twistInfo.getTwistedValue(fullurl);
if (twistedUrl != null) {
originalRequestUrlBeforeTwisting = realServiceUrl;
realServiceUrl = new Url(twistedUrl);
}
httpclient.getCredentialsProvider()
.setCredentials(proxyServer.getAuthScope(), proxyServer.getCredentials());
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyServer.getHttpHost());
}

ResponseFromService responseMessage = null;
try {
HttpHost htttphost = new HttpHost(realServiceUrl.getHost(),
realServiceUrl.getPort(), realServiceUrl.getScheme());
HttpHost htttphost = new HttpHost(realServiceUrl.getHost(), realServiceUrl.getPort(),
realServiceUrl.getScheme());

HttpResponse response = httpclient.execute(htttphost,
request.postToRealServer(realServiceUrl));
HttpResponse response = httpclient.execute(htttphost, request.postToRealServer(realServiceUrl));
if (response.getStatusLine().getStatusCode() == 302) {
log.debug("FYI: 302 redirect occuring from "
+ realServiceUrl.getFullUrl());
log.debug("FYI: 302 redirect occuring from " + realServiceUrl.getFullUrl());
}
responseMessage = new ResponseFromService(response);
responseMessage
.setOriginalRequestUrlBeforeTwisting(originalRequestUrlBeforeTwisting);
responseMessage.setRequestUrl(realServiceUrl);
} catch (Exception e) {
log.error(e);
throw new ClientExecuteProxyException(
"Unable to retrieve a response. ", realServiceUrl, e);
throw new ClientExecuteProxyException("Unable to retrieve a response. ", realServiceUrl, e);
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
Expand Down
31 changes: 16 additions & 15 deletions src/java/com/mockey/model/FulfilledClientRequest.java
Expand Up @@ -41,25 +41,27 @@
*/
public class FulfilledClientRequest implements PersistableItem {

private Long id;
private Long serviceId;
private String serviceName;
private String clientRequestBody;
private String clientRequestHeaders;
private String clientRequestParameters;
private String clientRequestCookies;
private String clientResponseCookies;
private String requestorIP;
private String rawRequest;
private String comment;
private Long id = null;
private Long serviceId = null;
private String serviceName = null;
private String clientRequestBody = null;
private String clientRequestHeaders = null;
private String clientRequestParameters = null;
private String clientRequestCookies = null;
private String clientResponseCookies = null;
private String requestorIP = null;
private String rawRequest = null;
private String comment = null;
private String originalUrlBeforeTwisting = null;
private int serviceResponseType = -1;
private ResponseFromService responseMessage;
private RequestInspectionResult requestInspectionResult;
private Date time = new Date();

/**
* Value of the response type, defining Static, Dynamic, or Proxy response.
* @return non-negative value if set.
* Value of the response type, defining Static, Dynamic, or Proxy response.
*
* @return non-negative value if set.
* @see com.mockey.model.Service#getServiceResponseType()
*/
public int getServiceResponseType() {
Expand All @@ -75,7 +77,6 @@ public void setServiceResponseType(int serviceResponseType) {
this.serviceResponseType = serviceResponseType;
}


/**
*
* @return optional comment about this request.
Expand Down Expand Up @@ -207,5 +208,5 @@ public RequestInspectionResult getRequestInspectionResult() {
public void setRequestInspectionResult(RequestInspectionResult requestInspectionResult) {
this.requestInspectionResult = requestInspectionResult;
}

}

0 comments on commit 16734ab

Please sign in to comment.