Skip to content

Commit

Permalink
Merge pull request #7 from danilodeLuca/master
Browse files Browse the repository at this point in the history
Parser Url
  • Loading branch information
luanpotter authored Nov 6, 2017
2 parents 3aae30f + 7c8fc1b commit 789acb4
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 37 deletions.
18 changes: 16 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
.idea/
**.classpath
**.project
*.settings/org.eclipse.jdt.core.prefs
*/target
target/
*.iml
.metadata/*
.metadata/
RemoteSystemsTempFiles/*
RemoteSystemsTempFiles/
*~
**.idea*
**.iml
*.ipr
*.iws
.DS_Store/*
.DS_Store/
.settings/
32 changes: 9 additions & 23 deletions src/main/java/xyz/luan/facade/HttpFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.AbstractMap.SimpleEntry;
Expand All @@ -12,26 +13,22 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HttpFacade {

private String method;
private String baseUrl;
private List<Entry<String, String>> headers, queries, formParams;
private List<Entry<String, String>> headers, formParams;
private Object body;
private boolean isGzip;
private Integer timeout = 3 * 60 * 1000;
private boolean followRedirects;
private boolean fixedSize = false;
private boolean storeContent = true;
private UrlFacade url;


public HttpFacade(String baseUrl) {
this.baseUrl = baseUrl;
public HttpFacade(String baseUrl) throws MalformedURLException {
this.url = new UrlFacade(baseUrl);
this.headers = new ArrayList<>();
this.queries = new ArrayList<>();
this.formParams = new ArrayList<>();
this.followRedirects = false;
this.storeContent = true;
Expand Down Expand Up @@ -72,7 +69,7 @@ public HttpFacade gzip(String acceptContent) {
}

public HttpFacade query(String k, String v) {
this.queries.add(new SimpleEntry<>(k, v));
this.url.getQueries().add(new SimpleEntry<>(k, v));
return this;
}

Expand All @@ -87,14 +84,8 @@ public HttpFacade withFixedSize() {
}

public HttpFacade user(String user, String pass) {
String token = user + ":" + pass;
Matcher m = Pattern.compile("([a-z0-9A-Z]*)://(.*)").matcher(baseUrl);
if (!m.matches()) {
baseUrl = token + "@" + baseUrl;
} else {
baseUrl = m.group(1) + "://" + token + "@" + m.group(2);
}
header("Authentication", "Basic " + Util.encodeBase64(token));
String token = url.user(user, pass);
header("Authentication", "Basic " + token);
return this;
}

Expand Down Expand Up @@ -125,8 +116,7 @@ public HttpURLConnection generateConnection() throws IOException {
}

public String getUrl() {
String queryStr = urlEncodeUTF8(queries);
return baseUrl + (queryStr.isEmpty() ? "" : "?" + queryStr);
return url.buildUrl();
}

public static String urlEncodeUTF8(String s) {
Expand Down Expand Up @@ -183,10 +173,6 @@ private String generateBody() {
return null;
}

public String getBaseUrl() {
return baseUrl;
}

public Response get() throws IOException {
return method("GET").req();
}
Expand Down
187 changes: 187 additions & 0 deletions src/main/java/xyz/luan/facade/UrlFacade.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package xyz.luan.facade;

import java.net.MalformedURLException;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class UrlFacade {
private static final String URL_SPLIT_REGEX = "^(([^:\\/?#]+):)?(\\/\\/([^\\/?#]*))?([^?#]*)(\\\\?([^#]*))?(#(.*))?";
private static final int NO_PORT = -1;
private static final String DEFAULT_PROTOCOL = "http";
private String urlParsed;
private String urlString;
private String protocol = DEFAULT_PROTOCOL;
private String auth;
private String host;
private int port = NO_PORT;
private String path = "";
private String query;

public UrlFacade(String urlString) throws MalformedURLException {
this.urlString = urlString;
parse();
}

private void parse() throws MalformedURLException {
checkProtocol();
Pattern p = Pattern.compile(URL_SPLIT_REGEX);
Matcher matcher = p.matcher(this.urlString);
if (matcher.find()) {
this.path = matcher.group(5);
this.query = matcher.group(7).replaceAll("\\?", "");

splitAuthHostPort(matcher.group(4));
}
this.urlParsed = getFullUrl();
}

public void splitAuthHostPort(String fullHost) throws MalformedURLException {
String[] splited = fullHost.split("@");
if (splited.length == 2) {
this.auth = splited[0];
int index = 1;
setHostAndPort(splited, index);
} else if (splited.length == 1) {
int index = 0;
setHostAndPort(splited, index);
}
}

private void setHostAndPort(String[] splited, int index) throws MalformedURLException {
String[] allHostAndPort = splited[index].split(":");
this.host = allHostAndPort[0];

if (allHostAndPort.length == 2) {
try {
this.port = Integer.parseInt(allHostAndPort[1]);
} catch (NumberFormatException e) {
throw new MalformedURLException("Invalid port number :" + allHostAndPort[1]);
}
}
}

private void checkProtocol() {
String[] protocolSplited = this.urlString.split("://");
if (protocolSplited.length < 2) {
this.protocol = DEFAULT_PROTOCOL;
this.urlString = this.protocol + "://" + this.urlString;
} else {
this.protocol = protocolSplited[0];
}
}

private String getFullUrl() {
StringBuilder sb = new StringBuilder();
sb.append(this.protocol).append("://");
if (hasAuth())
sb.append(this.auth).append("@");
sb.append(this.host);
if (hasPort())
sb.append(":").append(this.port);

sb.append(this.path);

return sb.toString();
}

private boolean hasPort() {
return this.port != NO_PORT;
}

private boolean hasAuth() {
return this.auth != null && this.auth.trim().length() > 0;
}

public List<Entry<String, String>> getQueries() {
List<Entry<String, String>> queries = new ArrayList<>();
String[] paramsAndValues = this.query.split("&");
for (String paramAndValue : paramsAndValues) {
if (paramAndValue == null || paramAndValue.trim().length() == 0)
continue;
String[] splited = paramAndValue.split("=");
if (splited.length == 2) {
queries.add(new SimpleEntry<>(splited[0], splited[1]));
} else {
queries.add(new SimpleEntry<>(splited[0], ""));
}
}
return queries;
}

public int getPort() {
return this.port;
}

public String getUrlString() {
return urlString;
}

public void setUrlString(String urlString) {
this.urlString = urlString;
}

public String getProtocol() {
return protocol;
}

public void setProtocol(String protocol) {
this.protocol = protocol;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public String getQuery() {
return query;
}

public void setQuery(String query) {
this.query = query;
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public void setPort(int port) {
this.port = port;
}

public void setAuth(String auth) {
this.auth = auth;
}

public String getAuth() {
return this.auth;
}

public String buildUrl() {
String queryStr = HttpFacade.urlEncodeUTF8(getQueries());
return getFullUrl() + (queryStr.isEmpty() ? "" : "?" + queryStr);
}

public String getUrlParsed() {
return urlParsed;
}

public String user(String user, String pass) {
String token = user + ":" + pass;
this.auth = token;
this.urlParsed = getFullUrl();
return Util.encodeBase64(token);
}

}
4 changes: 2 additions & 2 deletions src/test/java/xyz/luan/facade/Base64Test.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package xyz.luan.facade;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class Base64Test {

@Test
Expand Down
12 changes: 7 additions & 5 deletions src/test/java/xyz/luan/facade/HttpFacadeTest.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package xyz.luan.facade;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

import java.net.MalformedURLException;

import org.junit.Test;

public class HttpFacadeTest {

@Test
public void userTestNoProtocol() {
public void userTestNoProtocol() throws MalformedURLException {
HttpFacade facade = new HttpFacade("luan.xyz");
facade.user("luan", "mypass123");
assertEquals("luan:[email protected]", facade.getUrl());
assertEquals("http://luan:[email protected]", facade.getUrl());
assertEquals("Basic bHVhbjpteXBhc3MxMjM=", facade.header("Authentication"));
}

@Test
public void userTestWithProtocol() {
public void userTestWithProtocol() throws MalformedURLException {
HttpFacade facade = new HttpFacade("https://luan.xyz");
facade.user("luan", "mypass123");
assertEquals("https://luan:[email protected]", facade.getUrl());
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/xyz/luan/facade/IntegrationTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package xyz.luan.facade;

import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

import java.io.IOException;

import static org.junit.Assert.assertEquals;
import org.junit.Ignore;
import org.junit.Test;

public class IntegrationTest {

Expand Down
Loading

0 comments on commit 789acb4

Please sign in to comment.