Skip to content

Commit

Permalink
share one method in one client
Browse files Browse the repository at this point in the history
  • Loading branch information
ywang19 committed Apr 29, 2014
1 parent 5e67959 commit 2a24a47
Showing 1 changed file with 17 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class AmpliClient {

private HttpClient client = null;
/* current operation */
private volatile HttpUriRequest request;
private volatile HttpUriRequest method;
private int port;
private String host;
private String nsRoot;
Expand All @@ -54,20 +54,20 @@ public AmpliClient(HttpClient client, String host, int port, String nsRoot) {
}

public void dispose() {
request = null;
method = null;
HttpClientUtil.disposeHttpClient(client);
}

public void abort() {
if (request != null)
request.abort();
request = null;
if (method != null)
method.abort();
method = null;
}

public boolean login() throws IOException, HttpException {
String storageUrl = "http://" + this.host + ":" + this.port;

HttpHead method = HttpClientUtil.makeHttpHead(storageUrl);
method = HttpClientUtil.makeHttpHead(storageUrl);
HttpResponse response = null;
try {
response = client.execute(method);
Expand All @@ -89,15 +89,14 @@ public String StoreObject(String sourceFilename, String ampliNamespace,
AmpliException {
File file = new File(sourceFilename);

HttpPut method = null;
HttpResponse response = null;
try {
String storageUrl = "http://" + this.host + ":" + this.port
+ nsRoot;
method = HttpClientUtil.makeHttpPut(storageUrl + "/" + HttpClientUtil.encodeURL(ampliNamespace)
+ "/" + HttpClientUtil.encodeURL(ampliFilename));

method.setEntity(new FileEntity(file, "application/octet-stream"));
((HttpPut)method).setEntity(new FileEntity(file, "application/octet-stream"));

response = client.execute(method);

Expand Down Expand Up @@ -127,7 +126,6 @@ public String StoreObject(String sourceFilename, String ampliNamespace,
public String StoreStreamedObject(InputStream stream, long length,
String ampliNamespace, String ampliFilename) throws IOException,
HttpException, AmpliException {
HttpPut method = null;
HttpResponse response = null;
try {
String storageUrl = "http://" + this.host + ":" + this.port
Expand All @@ -143,7 +141,7 @@ public String StoreStreamedObject(InputStream stream, long length,
entity.setChunked(false);
}

method.setEntity(entity);
((HttpPut)method).setEntity(entity);

response = client.execute(method);

Expand Down Expand Up @@ -191,7 +189,6 @@ public String StoreObject(byte[] data, String ampliNamespace,
String ampliFilename) throws IOException, HttpException,
AmpliException {
// int len = data.length;
HttpPut method = null;
String storageUrl = "http://" + this.host + ":" + this.port + nsRoot;
method = HttpClientUtil.makeHttpPut(storageUrl + "/" + HttpClientUtil.encodeURL(ampliNamespace) + "/"
+ HttpClientUtil.encodeURL(ampliFilename));
Expand All @@ -200,7 +197,7 @@ public String StoreObject(byte[] data, String ampliNamespace,

HttpResponse response = null;
try {
method.setEntity(new ByteArrayEntity(data));
((HttpPut)method).setEntity(new ByteArrayEntity(data));
response = client.execute(method);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
return response.getFirstHeader("ETag").getValue();
Expand All @@ -222,7 +219,7 @@ public byte[] getObject(String namespace, String objName)
throws IOException, HttpException, AmpliException {
String storageUrl = "http://" + this.host + ":" + this.port + nsRoot;

HttpGet method = HttpClientUtil.makeHttpGet(storageUrl + "/" + HttpClientUtil.encodeURL(namespace)
method = HttpClientUtil.makeHttpGet(storageUrl + "/" + HttpClientUtil.encodeURL(namespace)
+ "/" + HttpClientUtil.encodeURL(objName));

HttpResponse response = null;
Expand All @@ -249,7 +246,7 @@ public InputStream getObjectAsStream(String namespace, String objName)
throws IOException, HttpException, AmpliException {
String storageUrl = "http://" + this.host + ":" + this.port + nsRoot;

HttpGet method = HttpClientUtil.makeHttpGet(storageUrl + "/" + HttpClientUtil.encodeURL(namespace)
method = HttpClientUtil.makeHttpGet(storageUrl + "/" + HttpClientUtil.encodeURL(namespace)
+ "/" + HttpClientUtil.encodeURL(objName));

HttpResponse response = null;
Expand All @@ -272,7 +269,6 @@ public InputStream getObjectAsStream(String namespace, String objName)
public boolean deleteObject(String ampliNamespace, String name)
throws HttpException, IOException, AmpliException {

HttpDelete method = null;
HttpResponse response = null;

try {
Expand Down Expand Up @@ -302,7 +298,6 @@ public boolean deleteObject(String ampliNamespace, String name)
public AmpliPolicy createPolicy(AmpliPolicy policy) throws HttpException,
IOException, AmpliException {

HttpPut method = null;
HttpResponse response = null;
try {
String storageUrl = "http://" + this.host + ":" + this.port
Expand Down Expand Up @@ -336,7 +331,6 @@ public AmpliPolicy createPolicy(AmpliPolicy policy) throws HttpException,
public AmpliPolicy getPolicy(String policyId) throws HttpException,
IOException, AmpliException {

HttpGet method = null;
HttpResponse response = null;
try {
String storageUrl = "http://" + this.host + ":" + this.port
Expand Down Expand Up @@ -366,13 +360,13 @@ public AmpliPolicy getPolicy(String policyId) throws HttpException,

public AmpliNamespace createNamespace(AmpliNamespace namespace)
throws HttpException, IOException, AmpliException {
HttpPut method = null;
HttpResponse response = null;

HttpResponse response = null;
try {
String storageUrl = "http://" + this.host + ":" + this.port
+ nsRoot;

method = new HttpPut(storageUrl);
method = HttpClientUtil.makeHttpGet(storageUrl);

method.setHeader("Content-Type", "text/plain");

Expand Down Expand Up @@ -405,7 +399,7 @@ public String createNamespace(String namespace, String policy_id)
String storageUrl = "http://" + this.host + ":" + this.port
+ nsRoot;

method = new HttpPut(storageUrl);
method = HttpClientUtil.makeHttpPut(storageUrl);

method.setHeader("Content-Type", "text/plain");

Expand Down Expand Up @@ -438,7 +432,6 @@ public String createNamespace(String namespace, String policy_id)
public AmpliNamespace getNamespace(String name) throws HttpException,
IOException, AmpliException {

HttpGet method = null;
HttpResponse response = null;
try {
String storageUrl = "http://" + this.host + ":" + this.port
Expand Down Expand Up @@ -469,7 +462,6 @@ public AmpliNamespace getNamespace(String name) throws HttpException,
public boolean isNamespaceExisted(String name) throws HttpException,
IOException, AmpliException {

HttpHead method = null;
HttpResponse response = null;
try {
String storageUrl = "http://" + this.host + ":" + this.port
Expand Down Expand Up @@ -497,7 +489,7 @@ public boolean isNamespaceExisted(String name) throws HttpException,

public boolean deleteNamespace(String namespace) throws HttpException,
IOException, AmpliException {
HttpDelete method = null;

HttpResponse response = null;
try {
String storageUrl = "http://" + this.host + ":" + this.port
Expand Down Expand Up @@ -525,7 +517,7 @@ public Map<String, String> getObjectMetadata(String namespace,
String objName) throws IOException, HttpException, AmpliException {
String storageUrl = "http://" + this.host + ":" + this.port + nsRoot;

HttpGet method = HttpClientUtil.makeHttpGet(storageUrl + "/" + HttpClientUtil.encodeURL(namespace)
method = HttpClientUtil.makeHttpGet(storageUrl + "/" + HttpClientUtil.encodeURL(namespace)
+ "/" + HttpClientUtil.encodeURL(objName) + "?meta=http");

HttpResponse response = null;
Expand Down

0 comments on commit 2a24a47

Please sign in to comment.