Skip to content

Commit

Permalink
urlencode parameter value in request
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasmoreau committed May 17, 2022
1 parent 06623f3 commit 2252b72
Showing 1 changed file with 22 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,55 @@

import org.vamdc.portal.Settings;



public class PostRequest implements Callable<URL>{
public class PostRequest implements Callable<URL> {

private URL consumer;
private Collection<URL> nodes;
public PostRequest(URL consumer, Collection<URL> nodes){

public PostRequest(URL consumer, Collection<URL> nodes) {
this.consumer = consumer;
this.nodes = nodes;
}

@Override
public URL call() throws Exception {

HttpURLConnection connection = setupConnection();

return processResponse(connection);
}

private URL processResponse(HttpURLConnection connection)
throws IOException, MalformedURLException {
URL result=null;
private URL processResponse(HttpURLConnection connection) throws IOException, MalformedURLException {
URL result = null;
int resultCode = connection.getResponseCode();

if (resultCode== HttpURLConnection.HTTP_MOVED_TEMP || resultCode== HttpURLConnection.HTTP_SEE_OTHER || resultCode==HttpURLConnection.HTTP_MOVED_PERM){
if (resultCode == HttpURLConnection.HTTP_MOVED_TEMP || resultCode == HttpURLConnection.HTTP_SEE_OTHER
|| resultCode == HttpURLConnection.HTTP_MOVED_PERM) {
result = new URL(connection.getHeaderField("Location"));
}else{
throw new IOException("XSAMS processor service returned "+resultCode+" status code.");
} else {
throw new IOException("XSAMS processor service returned " + resultCode + " status code.");
}
return result;
}

private HttpURLConnection setupConnection() throws IOException,
ProtocolException {
private HttpURLConnection setupConnection() throws IOException, ProtocolException {
HttpURLConnection connection = (HttpURLConnection) consumer.openConnection();
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setReadTimeout(Settings.HTTP_DATA_TIMEOUT.getInt());
connection.setDoOutput(true);

String data="";
for (URL node:nodes){
if (data.length()>0)
data+="&";
data+=URLEncoder.encode("url", "UTF-8") + "=" + URLEncoder.encode(node.toString(), "UTF-8");

};

String data = "";
for (URL node : nodes) {
if (data.length() > 0)
data += "&";
data += URLEncoder.encode("url", "UTF-8") + "=" + URLEncoder.encode(node.toString(), "UTF-8");
}
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(data);
wr.flush();
wr.close();

return connection;
}

}

0 comments on commit 2252b72

Please sign in to comment.