Skip to content

Commit

Permalink
added methods to construct URLs from host and path, to ensure path is…
Browse files Browse the repository at this point in the history
… correctly escaped
  • Loading branch information
fracpete committed Jul 13, 2020
1 parent fd188e4 commit 222d0b5
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
79 changes: 78 additions & 1 deletion src/main/java/com/github/fracpete/requests4j/Requests.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Requests.java
* Copyright (C) 2019 University of Waikato, Hamilton, NZ
* Copyright (C) 2019-2020 University of Waikato, Hamilton, NZ
*/

package com.github.fracpete.requests4j;
Expand Down Expand Up @@ -34,6 +34,17 @@ public static Request get() {
return new Request(GET);
}

/**
* Instantiates a new GET request.
*
* @param host the host (ie protocol, host, port)
* @param path the path (gets encoded correctly)
* @return the request
*/
public static Request get(String host, String path) throws MalformedURLException {
return get().url(host, path);
}

/**
* Instantiates a new GET request.
*
Expand Down Expand Up @@ -63,6 +74,17 @@ public static Request post() {
return new Request(POST);
}

/**
* Instantiates a new POST request.
*
* @param host the host (ie protocol, host, port)
* @param path the path (gets encoded correctly)
* @return the request
*/
public static Request post(String host, String path) throws MalformedURLException {
return post().url(host, path);
}

/**
* Instantiates a new POST request.
*
Expand Down Expand Up @@ -92,6 +114,17 @@ public static Request put() {
return new Request(PUT);
}

/**
* Instantiates a new PUT request.
*
* @param host the host (ie protocol, host, port)
* @param path the path (gets encoded correctly)
* @return the request
*/
public static Request put(String host, String path) throws MalformedURLException {
return put().url(host, path);
}

/**
* Instantiates a new PUT request.
*
Expand Down Expand Up @@ -121,6 +154,17 @@ public static Request patch() {
return new Request(PATCH);
}

/**
* Instantiates a new PATCH request.
*
* @param host the host (ie protocol, host, port)
* @param path the path (gets encoded correctly)
* @return the request
*/
public static Request patch(String host, String path) throws MalformedURLException {
return patch().url(host, path);
}

/**
* Instantiates a new PATCH request.
*
Expand Down Expand Up @@ -150,6 +194,17 @@ public static Request head() {
return new Request(HEAD);
}

/**
* Instantiates a new HEAD request.
*
* @param host the host (ie protocol, host, port)
* @param path the path (gets encoded correctly)
* @return the request
*/
public static Request head(String host, String path) throws MalformedURLException {
return head().url(host, path);
}

/**
* Instantiates a new HEAD request.
*
Expand Down Expand Up @@ -179,6 +234,17 @@ public static Request options() {
return new Request(OPTIONS);
}

/**
* Instantiates a new OPTIONS request.
*
* @param host the host (ie protocol, host, port)
* @param path the path (gets encoded correctly)
* @return the request
*/
public static Request options(String host, String path) throws MalformedURLException {
return options().url(host, path);
}

/**
* Instantiates a new OPTIONS request.
*
Expand Down Expand Up @@ -208,6 +274,17 @@ public static Request delete() {
return new Request(DELETE);
}

/**
* Instantiates a new DELETE request.
*
* @param host the host (ie protocol, host, port)
* @param path the path (gets encoded correctly)
* @return the request
*/
public static Request delete(String host, String path) throws MalformedURLException {
return delete().url(host, path);
}

/**
* Instantiates a new DELETE request.
*
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/github/fracpete/requests4j/request/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
Expand Down Expand Up @@ -175,6 +176,26 @@ public Request url(String url) throws MalformedURLException {
return this;
}

/**
* Sets the host/path to contact.
*
* @param host the host (ie protocol, host, port)
* @param path the path (gets encoded correctly)
* @return itself
* @throws MalformedURLException if invalid URL
*/
public Request url(String host, String path) throws MalformedURLException {
try {
m_URL = new URIBuilder(host)
.setPath(path)
.build().toURL();
}
catch (Exception e) {
throw new MalformedURLException("Invalid URI: " + host + "\n" + e);
}
return this;
}

/**
* Sets the URL to contact.
*
Expand Down

0 comments on commit 222d0b5

Please sign in to comment.