Skip to content

Commit

Permalink
0004351: Change registration to send request parameters as POST instead
Browse files Browse the repository at this point in the history
of GET
  • Loading branch information
erilong committed Apr 16, 2020
1 parent d143c52 commit 98efcef
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 16 deletions.
Expand Up @@ -24,12 +24,17 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;

import org.jumpmind.exception.HttpException;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.io.IoConstants;
import org.jumpmind.symmetric.service.IParameterService;
import org.jumpmind.symmetric.service.RegistrationNotOpenException;
import org.jumpmind.symmetric.service.RegistrationPendingException;
Expand Down Expand Up @@ -62,6 +67,8 @@ public class HttpIncomingTransport implements IIncomingTransport {
private String nodeId;

private String securityToken;

private Map<String, String> requestProperties;

public HttpIncomingTransport(HttpTransportManager httpTransportManager, HttpConnection connection, IParameterService parameterService) {
this.httpTransportManager = httpTransportManager;
Expand All @@ -70,6 +77,12 @@ public HttpIncomingTransport(HttpTransportManager httpTransportManager, HttpConn
this.httpTimeout = parameterService.getInt(ParameterConstants.TRANSPORT_HTTP_TIMEOUT);
}

public HttpIncomingTransport(HttpTransportManager httpTransportManager, HttpConnection connection, IParameterService parameterService,
Map<String, String> requestProperties) {
this(httpTransportManager, connection, parameterService);
this.requestProperties = requestProperties;
}

public HttpIncomingTransport(HttpTransportManager httpTransportManager, HttpConnection connection, IParameterService parameterService,
String nodeId, String securityToken) {
this(httpTransportManager, connection, parameterService);
Expand Down Expand Up @@ -113,10 +126,11 @@ public String getRedirectionUrl() {

@Override
public InputStream openStream() throws IOException {


applyRequestProperties();
boolean manualRedirects = parameterService.is(ParameterConstants.TRANSPORT_HTTP_MANUAL_REDIRECTS_ENABLED, true);
if (manualRedirects) {
connection = this.openConnectionCheckRedirects(connection);
connection = openConnectionCheckRedirects();
}

int code = connection.getResponseCode();
Expand Down Expand Up @@ -178,7 +192,7 @@ public Map<String, String> getHeaders() {
* @return
* @throws IOException
*/
private HttpConnection openConnectionCheckRedirects(HttpConnection connection) throws IOException
private HttpConnection openConnectionCheckRedirects() throws IOException
{
boolean redir;
int redirects = 0;
Expand Down Expand Up @@ -206,15 +220,46 @@ private HttpConnection openConnectionCheckRedirects(HttpConnection connection) t
connection = httpTransportManager.openConnection(target, nodeId, securityToken);
connection.setConnectTimeout(httpTimeout);
connection.setReadTimeout(httpTimeout);
applyRequestProperties();

redirects++;
}
} while (redir);

return connection;
}


protected void applyRequestProperties() throws IOException {
if (requestProperties != null) {
connection.setRequestMethod("POST");
connection.setDoOutput(true);
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
if (sb.length() != 0) {
sb.append("&");
}
sb.append(requestProperty.getKey()).append("=");
sb.append(URLEncoder.encode(requestProperty.getValue(), IoConstants.ENCODING));
}

try (OutputStream os = connection.getOutputStream()) {
PrintWriter pw = new PrintWriter(new OutputStreamWriter(os, IoConstants.ENCODING), true);
pw.println(sb.toString());
pw.flush();
}
}
}

public HttpConnection getConnection() {
return connection;
}

public Map<String, String> getRequestProperties() {
return requestProperties;
}

public void setRequestProperties(Map<String, String> requestProperties) {
this.requestProperties = requestProperties;
}

}
Expand Up @@ -309,7 +309,7 @@ public IIncomingTransport getConfigTransport(Node remote, Node local, String sec

public IIncomingTransport getRegisterTransport(Node node, String registrationUrl) throws IOException {
return new HttpIncomingTransport(this, createGetConnectionFor(new URL(buildRegistrationUrl(
registrationUrl, node))), engine.getParameterService());
registrationUrl, node))), engine.getParameterService(), buildRegistrationRequestProperties(node));
}

@Override
Expand All @@ -327,20 +327,25 @@ public static String buildRegistrationUrl(String baseUrl, Node node) throws IOEx
baseUrl = "";
}
StringBuilder builder = new StringBuilder(baseUrl);
builder.append("/registration?");
append(builder, WebConstants.NODE_GROUP_ID, node.getNodeGroupId());
append(builder, WebConstants.EXTERNAL_ID, node.getExternalId());
append(builder, WebConstants.SYNC_URL, node.getSyncUrl());
append(builder, WebConstants.SCHEMA_VERSION, node.getSchemaVersion());
append(builder, WebConstants.DATABASE_TYPE, node.getDatabaseType());
append(builder, WebConstants.DATABASE_VERSION, node.getDatabaseVersion());
append(builder, WebConstants.SYMMETRIC_VERSION, node.getSymmetricVersion());
append(builder, WebConstants.DEPLOYMENT_TYPE, node.getDeploymentType());
append(builder, WebConstants.HOST_NAME, AppUtils.getHostName());
append(builder, WebConstants.IP_ADDRESS, AppUtils.getIpAddress());
builder.append("/registration");
return builder.toString();
}

public static Map<String, String> buildRegistrationRequestProperties(Node node) {
Map<String, String> prop = new HashMap<String, String>();
prop.put(WebConstants.NODE_GROUP_ID, node.getNodeGroupId());
prop.put(WebConstants.EXTERNAL_ID, node.getExternalId());
prop.put(WebConstants.SYNC_URL, node.getSyncUrl());
prop.put(WebConstants.SCHEMA_VERSION, node.getSchemaVersion());
prop.put(WebConstants.DATABASE_TYPE, node.getDatabaseType());
prop.put(WebConstants.DATABASE_VERSION, node.getDatabaseVersion());
prop.put(WebConstants.SYMMETRIC_VERSION, node.getSymmetricVersion());
prop.put(WebConstants.DEPLOYMENT_TYPE, node.getDeploymentType());
prop.put(WebConstants.HOST_NAME, AppUtils.getHostName());
prop.put(WebConstants.IP_ADDRESS, AppUtils.getIpAddress());
return prop;
}

protected HttpConnection createGetConnectionFor(URL url, String nodeId, String securityToken) throws IOException {
HttpConnection conn = openConnection(url, nodeId, securityToken);
conn.setRequestProperty("accept-encoding", "gzip");
Expand Down

0 comments on commit 98efcef

Please sign in to comment.