Skip to content

Commit

Permalink
Adding signing work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
fkautz committed May 12, 2015
1 parent 499f4b6 commit aee19dc
Show file tree
Hide file tree
Showing 12 changed files with 539 additions and 50 deletions.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Sat May 02 19:44:35 PDT 2015
#Sun May 10 13:11:43 PDT 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/io/minio/objectstorage/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public interface Client {
public static String ACL_PRIVATE = "private";
public static String ACL_PUBLIC_READ = "public-read";
public static String ACL_PUBLIC_READ_WRITE = "public-read-write";
public static String ACL_AUTHENTICATED_READ = "authenticated-read";
public static String ACL_BUCKET_OWNER_READ = "bucket-owner-read";
public static String ACL_BUCKET_OWNER_FULL_CONTROL = "bucket-owner-full-control";
String ACL_PRIVATE = "private";
String ACL_PUBLIC_READ = "public-read";
String ACL_PUBLIC_READ_WRITE = "public-read-write";
String ACL_AUTHENTICATED_READ = "authenticated-read";
String ACL_BUCKET_OWNER_READ = "bucket-owner-read";
String ACL_BUCKET_OWNER_FULL_CONTROL = "bucket-owner-full-control";

URL getUrl();

Expand All @@ -49,5 +51,7 @@ public interface Client {
boolean createBucket(String bucket, String acl) throws IOException;

void createObject(String bucket, String key, String contentType, long size, InputStream data) throws IOException, XmlPullParserException;

void setKeys(String foo, String bar);
}

101 changes: 77 additions & 24 deletions src/main/java/io/minio/objectstorage/client/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,24 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

public class HttpClient implements Client {
private static final int PART_SIZE = 5 * 1024 * 1024;
private final URL url;
private HttpTransport transport = new NetHttpTransport();
private String accessKey;
private String secretKey;
private Logger logger;

HttpClient(URL url) {
this.url = url;
Expand All @@ -52,9 +60,17 @@ public URL getUrl() {
public ObjectMetadata getObjectMetadata(String bucket, String key) throws IOException {
GenericUrl url = getGenericUrlOfKey(bucket, key);

HttpRequestFactory requestFactory = this.transport.createRequestFactory();
HttpRequestFactory requestFactory = this.transport.createRequestFactory(new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
RequestSigner signer = new RequestSigner();
signer.setAccessKeys(accessKey, secretKey);
request.setInterceptor(signer);
}
});
HttpRequest httpRequest = requestFactory.buildGetRequest(url);
httpRequest = httpRequest.setRequestMethod("HEAD");
httpRequest.getHeaders().setUserAgent("Minio");
HttpResponse response = httpRequest.execute();
try {
HttpHeaders headers = response.getHeaders();
Expand Down Expand Up @@ -148,22 +164,34 @@ void setTransport(HttpTransport transport) {
public ListAllMyBucketsResult listBuckets() throws IOException, XmlPullParserException {
GenericUrl url = new GenericUrl(this.url);

HttpRequestFactory requestFactory = this.transport.createRequestFactory();
HttpRequestFactory requestFactory = this.transport.createRequestFactory(new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
RequestSigner signer = new RequestSigner();
signer.setAccessKeys(accessKey, secretKey);
request.setInterceptor(signer);
}
});
HttpRequest httpRequest = requestFactory.buildGetRequest(url);
httpRequest = httpRequest.setRequestMethod("GET");
httpRequest.getHeaders().setAccept("application/xml");
httpRequest.setFollowRedirects(false);
HttpResponse response = httpRequest.execute();
try {
XmlPullParser parser = Xml.createParser();
InputStreamReader reader = new InputStreamReader(response.getContent(), "UTF-8");
parser.setInput(reader);

XmlPullParser parser = Xml.createParser();
InputStreamReader reader = new InputStreamReader(response.getContent(), "UTF-8");
parser.setInput(reader);

ListAllMyBucketsResult result = new ListAllMyBucketsResult();

Xml.parseElement(parser, result, new XmlNamespaceDictionary(), null);
ListAllMyBucketsResult result = new ListAllMyBucketsResult();

return result;
Xml.parseElement(parser, result, new XmlNamespaceDictionary(), null);
return result;
} finally {
response.disconnect();
}
}


@Override
public boolean testBucketAccess(String bucket) throws IOException {
GenericUrl url = getGenericUrlOfBucket(bucket);
Expand All @@ -175,10 +203,7 @@ public boolean testBucketAccess(String bucket) throws IOException {
try {
HttpResponse response = httpRequest.execute();
try {
if (response.getStatusCode() == 200) {
return true;
}
return false;
return response.getStatusCode() == 200;
} finally {
response.disconnect();
}
Expand All @@ -198,10 +223,7 @@ public boolean createBucket(String bucket, String acl) throws IOException {
try {
HttpResponse execute = httpRequest.execute();
try {
if (execute.getStatusCode() == 200) {
return true;
}
return false;
return execute.getStatusCode() == 200;
} finally {
execute.disconnect();
}
Expand All @@ -216,7 +238,7 @@ public void createObject(String bucket, String key, String contentType, long siz
int partSize = 0;
String uploadID = null;

if (size > this.PART_SIZE) {
if (size > PART_SIZE) {
isMultipart = true;
partSize = computePartSize(size);
uploadID = newMultipartUpload(bucket, key);
Expand All @@ -238,6 +260,12 @@ public void createObject(String bucket, String key, String contentType, long siz
}
}

@Override
public void setKeys(String accessKey, String secretKey) {
this.accessKey = accessKey;
this.secretKey = secretKey;
}


private String newMultipartUpload(String bucket, String key) throws IOException, XmlPullParserException {
GenericUrl url = getGenericUrlOfKey(bucket, key);
Expand All @@ -262,15 +290,15 @@ private String newMultipartUpload(String bucket, String key) throws IOException,

private void completeMultipart(String bucket, String key, String uploadID, List<String> etags) throws IOException {
GenericUrl url = getGenericUrlOfKey(bucket, key);
url.set("uploadId" , uploadID);
url.set("uploadId", uploadID);

HttpRequestFactory requestFactory = this.transport.createRequestFactory();
HttpRequest httpRequest = requestFactory.buildGetRequest(url).setRequestMethod("POST");

List<Part> parts = new LinkedList<>();
for (int i = 0; i < etags.size(); i++) {
Part part = new Part();
part.setPartNumber(i+1);
part.setPartNumber(i + 1);
part.seteTag(etags.get(i));
parts.add(part);
}
Expand All @@ -287,7 +315,7 @@ private void completeMultipart(String bucket, String key, String uploadID, List<
}

private int computePartSize(long size) {
int minimumPartSize = this.PART_SIZE; // 5MB
int minimumPartSize = PART_SIZE; // 5MB
int partSize = (int) (size / 9999);
return Math.max(minimumPartSize, partSize);
}
Expand All @@ -300,8 +328,8 @@ private String putObject(String bucket, String key, String contentType, byte[] d
GenericUrl url = getGenericUrlOfKey(bucket, key);

if (partID > 0) {
url.set("partNumber" ,partID);
url.set("uploadId" , uploadId);
url.set("partNumber", partID);
url.set("uploadId", uploadId);
}

byte[] md5sum = null;
Expand Down Expand Up @@ -349,4 +377,29 @@ private byte[] readData(int size, InputStream data) throws IOException {

return fullData;
}

public void enableLogging() {
if (logger == null) {
logger = Logger.getLogger(HttpTransport.class.getName());
logger.setLevel(Level.CONFIG);
logger.addHandler(new Handler() {

@Override
public void close() throws SecurityException {
}

@Override
public void flush() {
}

@Override
public void publish(LogRecord record) {
// default ConsoleHandler will print >= INFO to System.err
if (record.getLevel().intValue() < Level.INFO.intValue()) {
System.out.println(record.getMessage());
}
}
});
}
}
}
51 changes: 51 additions & 0 deletions src/main/java/io/minio/objectstorage/client/Regions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Minimal Object Storage Library, (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.minio.objectstorage.client;

import java.util.HashMap;
import java.util.Map;

public enum Regions {
INSTANCE;
private final Map<String, String> regions = new HashMap<>();

Regions() {
// ap-northeast-1
regions.put("s3-ap-northeast-1.amazonaws.com", "ap-northeast-1");
// ap-southeast-1
regions.put("s3-ap-southeast-1.amazonaws.com", "ap-southeast-1");
// ap-southeast-2
regions.put("s3-ap-southeast-2.amazonaws.com", "ap-southeast-2");
// eu-central-1
regions.put("s3-eu-central-1.amazonaws.com", "eu-central-1");
// eu-west-1
regions.put("s3-eu-west-1.amazonaws.com", "eu-west-1");
// sa-east-1
regions.put("s3-sa-east-1.amazonaws.com", "sa-east-1");
// us-east-1
regions.put("s3.amazonaws.com", "us-east-1");
regions.put("s3-external-1.amazonaws.com", "us-east-1");
// us-west-1
regions.put("s3-us-west-1.amazonaws.com", "us-west-1");
// us-west-2
regions.put("s3-us-west-2.amazonaws.com", "us-west-2");
}

public String getRegion(String host) {
return regions.getOrDefault(host, "milkyway");
}
}
Loading

0 comments on commit aee19dc

Please sign in to comment.