Skip to content

Commit a6bf4c3

Browse files
committed
Initial commit
0 parents  commit a6bf4c3

5 files changed

Lines changed: 216 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
urlencoder.iml
3+
target/

pom.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>br.com.quintoandar</groupId>
8+
<artifactId>urlencoder</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>javax.ws.rs</groupId>
14+
<artifactId>javax.ws.rs-api</artifactId>
15+
<version>${rs-api.version}</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.apache.commons</groupId>
19+
<artifactId>commons-lang3</artifactId>
20+
<version>${commons-lang3.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.apache.httpcomponents</groupId>
24+
<artifactId>httpclient</artifactId>
25+
<version>${httpclient.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.jboss.resteasy</groupId>
29+
<artifactId>resteasy-jaxrs</artifactId>
30+
<version>${resteasy-jaxrs.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.jboss.resteasy</groupId>
34+
<artifactId>resteasy-client</artifactId>
35+
<version>${resteasy-client.version}</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.projectlombok</groupId>
39+
<artifactId>lombok</artifactId>
40+
<version>${lombok.version}</version>
41+
</dependency>
42+
</dependencies>
43+
44+
<properties>
45+
<rs-api.version>2.1</rs-api.version>
46+
<commons-lang3.version>3.7</commons-lang3.version>
47+
<httpclient.version>4.5.5</httpclient.version>
48+
<resteasy-jaxrs.version>3.1.4.Final</resteasy-jaxrs.version>
49+
<resteasy-client.version>3.1.4.Final</resteasy-client.version>
50+
<lombok.version>1.16.20</lombok.version>
51+
</properties>
52+
53+
</project>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package br.com.quintoandar.urlencoder;
2+
3+
import java.util.Map;
4+
import java.util.Random;
5+
import lombok.NonNull;
6+
import org.apache.commons.lang3.RandomStringUtils;
7+
8+
public class UrlEncoder {
9+
10+
private YourlsApiService service;
11+
private int keywordLength;
12+
private String signature;
13+
private boolean withEnvironment;
14+
private String environment;
15+
16+
public UrlEncoder(@NonNull String uri, @NonNull int keywordLength,
17+
@NonNull String signature) {
18+
this.service = new YourlsApiService(uri);
19+
this.keywordLength = keywordLength;
20+
this.signature = signature;
21+
this.withEnvironment = false;
22+
}
23+
24+
public UrlEncoder(String uri, int keywordLength, String signature,
25+
@NonNull String environment) {
26+
this(uri, keywordLength, signature);
27+
this.withEnvironment = true;
28+
this.environment = String.format("%s-", environment);
29+
}
30+
31+
public String encodeURL(@NonNull String urlToEncode) {
32+
return encodeURL(urlToEncode, null, false);
33+
}
34+
35+
public String encodeURLWithSufix(String urlToEncode, @NonNull String keyword) {
36+
return encodeURL(urlToEncode, keyword, true);
37+
}
38+
39+
private String encodeURL(String urlToEncode, String keyword, boolean overwrite) {
40+
keyword = keyword == null ? generateRandomAlphanumeric() : keyword;
41+
String keywordWithPrefix = this.environment + keyword;
42+
43+
keywordWithPrefix = keywordWithPrefix.toLowerCase();
44+
Map<String, Object> map = this.service.getInstance()
45+
.shorturl(signature, "shorturl", "json", urlToEncode, keywordWithPrefix,
46+
"URL Shortned via QuinToUrlEncoder.java");
47+
48+
if (map.get("status").equals("fail")) {
49+
if (map.get("message").toString().equalsIgnoreCase(
50+
"Short URL " + keywordWithPrefix + " already exists in database or is reserved")) {
51+
if (overwrite && keyword != null) {
52+
this.service.getInstance()
53+
.delete(signature, YourlsApi.ACTION_DELETE, YourlsApi.FORMAT_JSON, keywordWithPrefix);
54+
return encodeURL(urlToEncode, keyword, overwrite);
55+
}
56+
if (!overwrite && keyword == null) {
57+
return encodeURL(urlToEncode, null, overwrite);
58+
}
59+
}
60+
}
61+
62+
if (map != null && map.containsKey("shorturl")) {
63+
return map.get("shorturl").toString();
64+
}
65+
return null;
66+
}
67+
68+
public String encodeURLWithHash(String urlToEncode, String hashLoginBypass, String prefix) {
69+
int tamHashEncurtado = 6;
70+
StringBuilder keyword = new StringBuilder(prefix);
71+
72+
if (hashLoginBypass != null && hashLoginBypass.length() >= tamHashEncurtado) {
73+
int ini = (new Random()).nextInt(hashLoginBypass.length() - tamHashEncurtado);
74+
keyword.append("-" + hashLoginBypass.substring(ini, ini + tamHashEncurtado));
75+
}
76+
return encodeURL(urlToEncode, keyword.toString(), true);
77+
}
78+
79+
public String encodeWithMultipleTries(String urlToEncode, String hashLoginBypass, String prefix,
80+
int maxTries) {
81+
int tries = 0;
82+
while (tries < maxTries) {
83+
try {
84+
return encodeURLWithHash(urlToEncode, hashLoginBypass, prefix);
85+
} catch (Exception e) {
86+
tries++;
87+
}
88+
}
89+
throw new RuntimeException(
90+
String.format("Url shortning unavailable: tried %d", maxTries));
91+
}
92+
93+
private String generateRandomAlphanumeric() {
94+
return RandomStringUtils.randomAlphanumeric(keywordLength).toLowerCase();
95+
}
96+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package br.com.quintoandar.urlencoder;
2+
3+
import java.util.Map;
4+
5+
import javax.ws.rs.Consumes;
6+
import javax.ws.rs.DefaultValue;
7+
import javax.ws.rs.GET;
8+
import javax.ws.rs.Path;
9+
import javax.ws.rs.Produces;
10+
import javax.ws.rs.QueryParam;
11+
import javax.ws.rs.core.MediaType;
12+
13+
public interface YourlsApi {
14+
15+
String ACTION_SHORTURL = "shorturl";
16+
String FORMAT_JSON = "json";
17+
String ACTION_DELETE = "delete";
18+
19+
@GET
20+
@Path("/yourls-api.php")
21+
@Produces({MediaType.APPLICATION_JSON, "text/javascript",
22+
MediaType.APPLICATION_JSON + "; charset=UTF-8",
23+
"text/javascript; charset=UTF-8", "*/*"})
24+
@Consumes({MediaType.APPLICATION_FORM_URLENCODED})
25+
Map<String, Object> shorturl(@QueryParam("signature") String signature,
26+
@QueryParam("action") @DefaultValue(ACTION_SHORTURL) String action,
27+
@QueryParam("format") @DefaultValue(FORMAT_JSON) String format, @QueryParam("url") String url,
28+
@QueryParam("keyword") String keyword, @QueryParam("title") String title);
29+
30+
@GET
31+
@Path("/yourls-api.php")
32+
@Produces({MediaType.APPLICATION_JSON, "text/javascript",
33+
MediaType.APPLICATION_JSON + "; charset=UTF-8",
34+
"text/javascript; charset=UTF-8", "*/*"})
35+
@Consumes({MediaType.APPLICATION_FORM_URLENCODED})
36+
Map<String, Object> delete(@QueryParam("signature") String signature,
37+
@QueryParam("action") @DefaultValue(ACTION_DELETE) String action,
38+
@QueryParam("format") @DefaultValue(FORMAT_JSON) String format,
39+
@QueryParam("shorturl") String keyword);
40+
41+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package br.com.quintoandar.urlencoder;
2+
3+
import lombok.Getter;
4+
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
5+
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
6+
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
7+
8+
public class YourlsApiService {
9+
10+
@Getter
11+
private YourlsApi instance;
12+
13+
public YourlsApiService(String uri) {
14+
this.instance = createService(uri);
15+
}
16+
17+
private YourlsApi createService(String uri) {
18+
ResteasyClient client = new ResteasyClientBuilder().build();
19+
ResteasyWebTarget target = client.target(uri);
20+
return target.proxy(YourlsApi.class);
21+
}
22+
23+
}

0 commit comments

Comments
 (0)