-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
hehe.thh
committed
Apr 16, 2018
1 parent
5a1d11f
commit 703c06c
Showing
5 changed files
with
144 additions
and
16 deletions.
There are no files selected for viewing
14 changes: 4 additions & 10 deletions
14
src/main/java/com/aliyuncs/fc/client/FunctionComputeClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.aliyuncs.fc.utils; | ||
|
||
import java.net.URI; | ||
|
||
import static com.google.common.base.Strings.isNullOrEmpty; | ||
|
||
public class UriBuilder { | ||
private String scheme; | ||
private String host; | ||
private int port; | ||
private String path; | ||
private StringBuilder query; | ||
private String fragment; | ||
|
||
private UriBuilder(URI uri) { | ||
this(uri.getScheme(), uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment()); | ||
} | ||
|
||
private UriBuilder(String scheme, String host, int port, String path, String query, String fragment) { | ||
this.scheme = scheme; | ||
this.host = host; | ||
this.port = port; | ||
this.path = path; | ||
this.query = query == null ? new StringBuilder() : new StringBuilder(query); | ||
this.fragment = fragment; | ||
} | ||
|
||
public static UriBuilder fromUri(URI uri) { | ||
return new UriBuilder(uri); | ||
} | ||
|
||
public UriBuilder queryParam(String name, String value) { | ||
if (query.length() != 0) { | ||
query.append("&"); | ||
} | ||
|
||
query.append(name).append("=").append(value); | ||
|
||
return this; | ||
} | ||
|
||
public String build() { | ||
StringBuilder builder = new StringBuilder(); | ||
|
||
if ( ! isNullOrEmpty(scheme) ) { | ||
builder.append(scheme).append("://"); | ||
} | ||
|
||
if ( ! isNullOrEmpty(host) ) { | ||
builder.append(host); | ||
} | ||
|
||
if ( port != -1 ) { | ||
builder.append(":").append(port); | ||
} | ||
|
||
if ( ! isNullOrEmpty(path) ) { | ||
builder.append(path); | ||
} | ||
|
||
if ( query.length() != 0 ) { | ||
builder.append("?").append(query.toString()); | ||
} | ||
|
||
if ( ! isNullOrEmpty(fragment) ) { | ||
builder.append("#").append(fragment); | ||
} | ||
|
||
return builder.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.aliyuncs.fc.utils; | ||
|
||
import org.junit.Test; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class UriBuilderTest { | ||
|
||
@Test | ||
public void testBuildUri() throws URISyntaxException { | ||
UriBuilder uriBuilder = UriBuilder.fromUri(new URI("http://www.test.com")); | ||
|
||
assertEquals("http://www.test.com", uriBuilder.build()); | ||
|
||
uriBuilder = UriBuilder.fromUri(new URI("www.test.com")); | ||
|
||
assertEquals("www.test.com", uriBuilder.build()); | ||
|
||
uriBuilder = UriBuilder.fromUri(new URI("http://123.fc.shanghai.aliyuncs.com:90/version/proxy/service/function/action/action?p=1&v=2#123")); | ||
|
||
assertEquals("http://123.fc.shanghai.aliyuncs.com:90/version/proxy/service/function/action/action?p=1&v=2#123", uriBuilder.build()); | ||
|
||
uriBuilder = UriBuilder.fromUri(new URI("http://123.fc.shanghai.aliyuncs.com:90/version/proxy/service/function/action/action?p=1&v=2#123")) | ||
.queryParam("testParam1", "testValue1") | ||
.queryParam("testParam2", "testValue2"); | ||
|
||
assertEquals("http://123.fc.shanghai.aliyuncs.com:90/version/proxy/service/function/action/action?p=1&v=2&testParam1=testValue1&testParam2=testValue2#123", uriBuilder.build()); | ||
|
||
|
||
uriBuilder = UriBuilder.fromUri(new URI("http://123.fc.shanghai.aliyuncs.com/version/proxy/service/function/action/action#123")) | ||
.queryParam("testParam1", "testValue1") | ||
.queryParam("testParam2", "testValue2"); | ||
|
||
assertEquals("http://123.fc.shanghai.aliyuncs.com/version/proxy/service/function/action/action?testParam1=testValue1&testParam2=testValue2#123", uriBuilder.build()); | ||
|
||
uriBuilder = UriBuilder.fromUri(new URI("http://123.fc.shanghai.aliyuncs.com/version/proxy/service/function/action/action")); | ||
|
||
assertEquals("http://123.fc.shanghai.aliyuncs.com/version/proxy/service/function/action/action", uriBuilder.build()); | ||
} | ||
} |