Skip to content

Commit

Permalink
Refactor parametersToSkip (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsm5 authored and gvanbrakel committed Sep 10, 2019
1 parent e5040ea commit 600818f
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2017 - 2018 Nationale-Nederlanden
Copyright 2017-2019 Nationale-Nederlanden
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -161,7 +161,7 @@ public String sendMessageWithTimeoutGuarded(String correlationID, String path, P
}

@Override
public HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueList parameters, Map<String, String> headersParamsMap, IPipeLineSession session) throws SenderException {
public HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueList parameters, IPipeLineSession session) throws SenderException {

NetStorageAction netStorageAction = new NetStorageAction(getAction());
netStorageAction.setVersion(actionVersion);
Expand All @@ -184,7 +184,7 @@ public HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueL

if (getMethodType().equals("GET")) {
if (parameters!=null) {
queryParametersAppended = appendParameters(queryParametersAppended,path,parameters,headersParamsMap);
queryParametersAppended = appendParameters(queryParametersAppended,path,parameters);
log.debug(getLogPrefix()+"path after appending of parameters ["+path.toString()+"]");
}
HttpGet method = new HttpGet(uri.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public CmisHttpSender() {
}

@Override
public HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueList pvl, Map<String, String> headersParamsMap, IPipeLineSession session) throws SenderException {
public HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueList pvl, IPipeLineSession session) throws SenderException {
HttpRequestBase method = null;

String methodType = (String) session.get("method");
Expand Down
29 changes: 14 additions & 15 deletions core/src/main/java/nl/nn/adapterframework/http/HttpSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.mail.BodyPart;
import javax.mail.MessagingException;
Expand Down Expand Up @@ -183,14 +182,14 @@ public void configure() throws ConfigurationException {
setContentType("text/html; charset="+getCharSet());
}

protected HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueList parameters, Map<String, String> headersParamsMap, IPipeLineSession session) throws SenderException {
protected HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueList parameters, IPipeLineSession session) throws SenderException {
if(isParamsInUrl())
return getMethod(uri, message, parameters, headersParamsMap);
return getMethod(uri, message, parameters);
else
return getPostMethodWithParamsInBody(uri, message, parameters, headersParamsMap, session);
return getPostMethodWithParamsInBody(uri, message, parameters, session);
}

protected HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueList parameters, Map<String, String> headersParamsMap) throws SenderException {
protected HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueList parameters) throws SenderException {
try {
boolean queryParametersAppended = false;

Expand All @@ -208,7 +207,7 @@ protected HttpRequestBase getMethod(URIBuilder uri, String message, ParameterVal

if (getMethodType().equals("GET")) {
if (parameters!=null) {
queryParametersAppended = appendParameters(queryParametersAppended,path,parameters,headersParamsMap);
queryParametersAppended = appendParameters(queryParametersAppended,path,parameters);
if (log.isDebugEnabled()) log.debug(getLogPrefix()+"path after appending of parameters ["+path.toString()+"]");
}
HttpGet method = new HttpGet(path+(parameters==null? message:""));
Expand All @@ -219,7 +218,7 @@ protected HttpRequestBase getMethod(URIBuilder uri, String message, ParameterVal
HttpPost method = new HttpPost(path.toString());
if (parameters!=null) {
StringBuffer msg = new StringBuffer(message);
appendParameters(true,msg,parameters,headersParamsMap);
appendParameters(true,msg,parameters);
if (StringUtils.isEmpty(message) && msg.length()>1) {
message=msg.substring(1);
} else {
Expand All @@ -234,7 +233,7 @@ protected HttpRequestBase getMethod(URIBuilder uri, String message, ParameterVal
HttpPut method = new HttpPut(path.toString());
if (parameters!=null) {
StringBuffer msg = new StringBuffer(message);
appendParameters(true,msg,parameters,headersParamsMap);
appendParameters(true,msg,parameters);
if (StringUtils.isEmpty(message) && msg.length()>1) {
message=msg.substring(1);
} else {
Expand Down Expand Up @@ -270,7 +269,7 @@ protected HttpRequestBase getMethod(URIBuilder uri, String message, ParameterVal
}
}

protected HttpPost getPostMethodWithParamsInBody(URIBuilder uri, String message, ParameterValueList parameters, Map<String, String> headersParamsMap, IPipeLineSession session) throws SenderException {
protected HttpPost getPostMethodWithParamsInBody(URIBuilder uri, String message, ParameterValueList parameters, IPipeLineSession session) throws SenderException {
try {
HttpPost hmethod = new HttpPost(uri.build());

Expand All @@ -287,8 +286,8 @@ protected HttpPost getPostMethodWithParamsInBody(URIBuilder uri, String message,
String name = pv.getDefinition().getName();
String value = pv.asStringValue("");

// Skip parameters that have been added as headers
if (headersParamsMap.keySet().contains(name))
// Skip parameters that are configured as ignored
if (skipParameter(name))
continue;

requestFormElements.add(new BasicNameValuePair(name,value));
Expand All @@ -302,7 +301,7 @@ protected HttpPost getPostMethodWithParamsInBody(URIBuilder uri, String message,
}
}
else {
HttpEntity requestEntity = createMultiPartEntity(message, parameters, headersParamsMap, session);
HttpEntity requestEntity = createMultiPartEntity(message, parameters, session);
hmethod.setEntity(requestEntity);
}
return hmethod;
Expand Down Expand Up @@ -345,7 +344,7 @@ protected FormBodyPart createMultipartBodypart(String name, InputStream is, Stri
return bodyPart.build();
}

protected HttpEntity createMultiPartEntity(String message, ParameterValueList parameters, Map<String, String> headersParamsMap, IPipeLineSession session) throws SenderException {
protected HttpEntity createMultiPartEntity(String message, ParameterValueList parameters, IPipeLineSession session) throws SenderException {
MultipartEntityBuilder entity = MultipartEntityBuilder.create();

entity.setCharset(Charset.forName(getCharSet()));
Expand All @@ -362,8 +361,8 @@ protected HttpEntity createMultiPartEntity(String message, ParameterValueList pa
String paramType = pv.getDefinition().getType();
String name = pv.getDefinition().getName();

// Skip parameters that have been added as headers
if (headersParamsMap.keySet().contains(name))
// Skip parameters that are configured as ignored
if (skipParameter(name))
continue;


Expand Down
65 changes: 40 additions & 25 deletions core/src/main/java/nl/nn/adapterframework/http/HttpSenderBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public abstract class HttpSenderBase extends TimeoutGuardSenderWithParametersBas
private boolean verifyHostname=true;
private boolean ignoreCertificateExpiredException=false;

private String headersParams=null;
private String headersParams="";
private boolean followRedirects=true;
private boolean staleChecking=true;
private int staleTimeout = 5000;
Expand All @@ -222,14 +222,23 @@ public abstract class HttpSenderBase extends TimeoutGuardSenderWithParametersBas
protected URIBuilder staticUri;
private CredentialFactory credentials;

private Set<Parameter> parametersToSkip=new HashSet<Parameter>();
private Set<String> parametersToSkip=new HashSet<String>();

protected void addParameterToSkip(Parameter param) {
if (param!=null) {
parametersToSkip.add(param);
protected void addParameterToSkip(String parameterName) {
if (parameterName != null) {
parametersToSkip.add(parameterName);
}
}

protected boolean skipParameter(String parameterName) {
for(String param : parametersToSkip) {
if(param.equalsIgnoreCase(parameterName))
return true;
}

return false;
}

protected URIBuilder getURI(String url) throws URISyntaxException {
URIBuilder uri = new URIBuilder(url);

Expand Down Expand Up @@ -274,7 +283,14 @@ public void configure() throws ConfigurationException {
paramList.configure();
if (StringUtils.isNotEmpty(getUrlParam())) {
urlParameter = paramList.findParameter(getUrlParam());
addParameterToSkip(urlParameter);
if(urlParameter != null)
addParameterToSkip(urlParameter.getName());
}

//Add all HeaderParams to paramIgnoreList
StringTokenizer st = new StringTokenizer(getHeadersParams(), ",");
while (st.hasMoreElements()) {
addParameterToSkip(st.nextToken());
}
}
if (getMaxConnections() <= 0) {
Expand Down Expand Up @@ -478,31 +494,29 @@ public boolean isSynchronous() {
return true;
}

protected boolean appendParameters(boolean parametersAppended, StringBuffer path, ParameterValueList parameters, Map<String, String> headersParamsMap) throws SenderException {
protected boolean appendParameters(boolean parametersAppended, StringBuffer path, ParameterValueList parameters) throws SenderException {
if (parameters != null) {
if (log.isDebugEnabled()) log.debug(getLogPrefix()+"appending ["+parameters.size()+"] parameters");
}
for(int i=0; i < parameters.size(); i++) {
if (parametersToSkip.contains(paramList.get(i))) {
if (skipParameter(paramList.get(i).getName())) {
if (log.isDebugEnabled()) log.debug(getLogPrefix()+"skipping ["+paramList.get(i)+"]");
continue;
}
ParameterValue pv = parameters.getParameterValue(i);
if (!headersParamsMap.keySet().contains(pv.getDefinition().getName())) {
try {
if (parametersAppended) {
path.append("&");
} else {
path.append("?");
parametersAppended = true;
}

String parameterToAppend = pv.getDefinition().getName() +"="+ URLEncoder.encode(pv.asStringValue(""), getCharSet());
if (log.isDebugEnabled()) log.debug(getLogPrefix()+"appending parameter ["+parameterToAppend+"]");
path.append(parameterToAppend);
} catch (UnsupportedEncodingException e) {
throw new SenderException(getLogPrefix()+"["+getCharSet()+"] encoding error. Failed to add parameter ["+pv.getDefinition().getName()+"]");
try {
if (parametersAppended) {
path.append("&");
} else {
path.append("?");
parametersAppended = true;
}

String parameterToAppend = pv.getDefinition().getName() +"="+ URLEncoder.encode(pv.asStringValue(""), getCharSet());
if (log.isDebugEnabled()) log.debug(getLogPrefix()+"appending parameter ["+parameterToAppend+"]");
path.append(parameterToAppend);
} catch (UnsupportedEncodingException e) {
throw new SenderException(getLogPrefix()+"["+getCharSet()+"] encoding error. Failed to add parameter ["+pv.getDefinition().getName()+"]");
}
}
return parametersAppended;
Expand All @@ -518,7 +532,7 @@ protected boolean appendParameters(boolean parametersAppended, StringBuffer path
* @return a {@link HttpRequestBase HttpRequest} object
* @throws SenderException
*/
protected abstract HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueList parameters, Map<String, String> headersParamsMap, IPipeLineSession session) throws SenderException;
protected abstract HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueList parameters, IPipeLineSession session) throws SenderException;

/**
* Custom implementation to extract the response and format it to a String result. <br/>
Expand Down Expand Up @@ -554,9 +568,10 @@ public String sendMessageWithTimeoutGuarded(String correlationID, String message

httpTarget = new HttpHost(uri.getHost(), getPort(uri), uri.getScheme());

// Resolve HeaderParameters
Map<String, String> headersParamsMap = new HashMap<String, String>();
if (headersParams != null) {
StringTokenizer st = new StringTokenizer(headersParams, ",");
StringTokenizer st = new StringTokenizer(getHeadersParams(), ",");
while (st.hasMoreElements()) {
String paramName = st.nextToken();
ParameterValue paramValue = pvl.getParameterValue(paramName);
Expand All @@ -569,7 +584,7 @@ public String sendMessageWithTimeoutGuarded(String correlationID, String message
message = URLEncoder.encode(message, getCharSet());
}

httpRequestBase = getMethod(uri, message, pvl, headersParamsMap, (prc==null) ? null : prc.getSession());
httpRequestBase = getMethod(uri, message, pvl, (prc==null) ? null : prc.getSession());
if(httpRequestBase == null)
throw new MethodNotSupportedException("could not find implementation for method ["+getMethodType()+"]");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2013, 2017-2018 Nationale-Nederlanden
Copyright 2013, 2017-2019 Nationale-Nederlanden
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,6 @@
package nl.nn.adapterframework.http;

import java.io.IOException;
import java.util.Map;

import nl.nn.adapterframework.configuration.ConfigurationException;
import nl.nn.adapterframework.configuration.ConfigurationWarnings;
Expand Down Expand Up @@ -85,9 +84,11 @@ public void configure() throws ConfigurationException {

if (paramList!=null && StringUtils.isNotEmpty(getSoapActionParam())) {
soapActionParameter=paramList.findParameter(getSoapActionParam());
if(soapActionParameter != null)
addParameterToSkip(soapActionParameter.getName());
serviceNamespaceURIParameter=paramList.findParameter(getServiceNamespaceParam());
addParameterToSkip(soapActionParameter);
addParameterToSkip(serviceNamespaceURIParameter);
if(serviceNamespaceURIParameter != null)
addParameterToSkip(serviceNamespaceURIParameter.getName());
}

if (StringUtils.isNotEmpty(getWssAuthAlias()) ||
Expand All @@ -98,13 +99,13 @@ public void configure() throws ConfigurationException {
}

@Override
protected HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueList parameters, Map<String,String> headersParamsMap) throws SenderException {
protected HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueList parameters) throws SenderException {
setContentType("text/xml; charset="+Misc.DEFAULT_INPUT_STREAM_ENCODING);
return super.getMethod(uri, message, parameters, headersParamsMap);
return super.getMethod(uri, message, parameters);
}

@Override
protected HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueList parameters, Map<String, String> headersParamsMap, IPipeLineSession session) throws SenderException {
protected HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueList parameters, IPipeLineSession session) throws SenderException {

String serviceNamespaceURI;
if (serviceNamespaceURIParameter!=null) {
Expand Down Expand Up @@ -132,7 +133,7 @@ protected HttpRequestBase getMethod(URIBuilder uri, String message, ParameterVal
}
if (log.isDebugEnabled()) log.debug(getLogPrefix()+"SOAPMSG [" + soapmsg + "]");

HttpRequestBase method = super.getMethod(uri, soapmsg, parameters, headersParamsMap, session);
HttpRequestBase method = super.getMethod(uri, soapmsg, parameters, session);
log.debug(getLogPrefix()+"setting SOAPAction header ["+soapActionURI+"]");
method.setHeader("SOAPAction", soapActionURI);
return method;
Expand Down
46 changes: 46 additions & 0 deletions core/src/test/java/nl/nn/adapterframework/http/HttpSenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,50 @@ public void simpleMockedHttpMtom() throws Throwable {
}
}
}

@Test
public void parametersToSkip() throws Throwable {
HttpSender sender = getSender();
String input = "<xml>input</xml>";

try {
IPipeLineSession pls = new PipeLineSessionBase(session);
ParameterResolutionContext prc = new ParameterResolutionContext(input, pls);

sender.setMethodType("POST");
sender.setParamsInUrl(false);
sender.setInputMessageParam("request");

String xmlMultipart = "<parts><part type=\"file\" name=\"document.pdf\" "
+ "sessionKey=\"part_file\" size=\"72833\" "
+ "mimeType=\"application/pdf\"/></parts>";
pls.put("multipartXml", xmlMultipart);
pls.put("part_file", new ByteArrayInputStream("<dummy xml file/>".getBytes()));

sender.setMtomEnabled(true);
sender.setMultipartXmlSessionKey("multipartXml");

Parameter urlParam = new Parameter();
urlParam.setName("url");
urlParam.setValue("http://ignore.me");
sender.addParameter(urlParam);

Parameter partParam = new Parameter();
partParam.setName("my-beautiful-part");
partParam.setValue("<partContent/>");
sender.addParameter(partParam);

sender.configure();
sender.open();

String result = sender.sendMessage(null, input, prc);
assertEquals(getFile("parametersToSkip.txt"), result.trim());
} catch (SenderException e) {
throw e.getCause();
} finally {
if (sender != null) {
sender.close();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
POST http://ignore.me HTTP/1.1
Message-Id: testmessageac13ecb1--30fe9225_16caa708707_-7fb1
custom-header: value
Content-Type: multipart/related; boundary="IGNORE"; charset=UTF-8; type="application/xop+xml"; start="<request>"; start-info="text/xml"

--IGNORE
Content-Type: application/xop+xml; charset=UTF-8
Content-Transfer-Encoding: 8bit
Content-ID: <request>

<xml>input</xml>
--IGNORE
Content-Type: application/xop+xml; charset=UTF-8
Content-Transfer-Encoding: 8bit
Content-ID: <my-beautiful-part>

<partContent/>
--IGNORE
Content-Type: application/pdf; charset=UTF-8
Content-Transfer-Encoding: binary
Content-Disposition: attachment; name="part_file"; filename="document.pdf"
Content-ID: <document.pdf>

<dummy xml file/>
--IGNORE--

0 comments on commit 600818f

Please sign in to comment.