encode and decode URLs in Java

This commit is contained in:
Umesh Awasthi
2019-05-30 21:44:05 -07:00
parent 7ae3e1626f
commit 6efd697a6c
5 changed files with 204 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javadevjournal</groupId>
<artifactId>java-encode-decode</artifactId>
<version>1.0-SNAPSHOT</version>
<name>java-encode-decode</name>
<url>https://www.javadevjournal.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@@ -0,0 +1,13 @@
package com.javadevjournal;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}

View File

@@ -0,0 +1,12 @@
package com.javadevjournal;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public class JavaDecodeExample {
public static String encodeData(String value) throws UnsupportedEncodingException {
return URLEncoder.encode(value, StandardCharsets.UTF_8.name());
}
}

View File

@@ -0,0 +1,58 @@
package com.javadevjournal;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
public class JavaDecodeExampleTest {
/*
expected values
"name","Java Dev Journal"
"email","contact-us@javadevjournal.com"
"secretKey","test_6H!7&DCepBtGGx-b"
*/
@Test
public void test_url_decoder() throws MalformedURLException {
URL url =new URL("https://www.javadevjournal.com?secretKey=test_6H%217%26DCepBtGGx-b&name=Java+Dev+Journal&email=contact-us%40javadevjournal.com");
Map<String, List<String>> decodedParams= decodeQuery(url);
System.out.println(decodedParams);
}
public Map<String, List<String>> decodeQuery(URL url) {
return Arrays.stream(url.getQuery().split("&"))
.map(this::splitQueryParameter)
.collect(Collectors.groupingBy(AbstractMap.SimpleImmutableEntry::getKey, LinkedHashMap::new, mapping(Map.Entry::getValue, toList())));
}
public AbstractMap.SimpleImmutableEntry<String, String> splitQueryParameter(String it) {
final int idx = it.indexOf("=");
final String key = idx > 0 ? it.substring(0, idx) : it;
final String value = idx > 0 && it.length() > idx + 1 ? it.substring(idx + 1) : null;
return new AbstractMap.SimpleImmutableEntry<>(key, decode(value));
}
private String decode(String value) {
try {
return URLDecoder.decode(value, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return value;
}
}

View File

@@ -0,0 +1,47 @@
package com.javadevjournal;
import org.junit.Test;
import org.junit.Assert;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
public class JavaEncodeExampleTest {
String expectedURL="https://www.javadevjournal.com?secretKey=test_6H%217%26DCepBtGGx-b&name=Java+Dev+Journal&email=contact-us%40javadevjournal.com"; //final encoded URL
@Test
public void test_url_encoding(){
Map<String, String> params = new HashMap<>(); //query string parameters
params.put("name","Java Dev Journal");
params.put("email","contact-us@javadevjournal.com");
params.put("secretKey","test_6H!7&DCepBtGGx-b");
String encodedURL = params.entrySet()
.stream()
.map(e->e.getKey() + "=" +encode(e.getValue()))
.collect(Collectors.joining("&","https://www.javadevjournal.com?",""));
assertThat(expectedURL,is(encodedURL));
}
public static String encode(String value) {
try {
return URLEncoder.encode(value, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
}