/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upenn.library.fcrepo.connector.annex;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import java.util.Properties;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.S3ClientOptions;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class S3AnnexResolverFactory implements AnnexRemoteBinaryResolver.AnnexResolverFactory {

    private static final String ACCESS_KEY_PROPNAME = "access_key";
    private static final String SECRET_KEY_PROPNAME = "secret_key";
    private static final String BUCKET_PROPNAME = "bucket";
    private static final String ENDPOINT_PROPNAME = "endpoint";
    
    private String bucket;
    private String accessKey;
    private String secretKey;
    private AmazonS3 conn;

    @Override
    public AnnexResolver getAnnexResolver(String annexId) {
        return new S3AnnexResolver(annexId, this);
    }

    @Override
    public void initialize(Properties props) {
        this.accessKey = props.getProperty(ACCESS_KEY_PROPNAME);
        this.secretKey = props.getProperty(SECRET_KEY_PROPNAME);
        this.bucket = props.getProperty(BUCKET_PROPNAME);
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);

        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setProtocol(Protocol.HTTP);
        S3ClientOptions clientOptions = new S3ClientOptions();
        clientOptions.setPathStyleAccess(true);

        conn = new AmazonS3Client(credentials, clientConfig);
        conn.setS3ClientOptions(clientOptions);
        conn.setEndpoint(props.getProperty(ENDPOINT_PROPNAME));
    }

    public URI getObjectURI(String annexId) {
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket, annexId);
        try {
            return conn.generatePresignedUrl(request).toURI();
        } catch (URISyntaxException ex) {
            throw null;
        }
    }

}

@@ -3,188 +3,626 @@
 %0A * 
-To change this license header, choose License Headers in Project Properties.%0A * To change this template file, choose Tools %7C Templates%0A * and open the template in the editor
+Copyright 2016 The Trustees of the University of Pennsylvania%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License
 .%0A */%0A
-%0A
 pack
@@ -1090,16 +1090,16 @@
 et.URI;%0A
+
 import j
@@ -1130,73 +1130,8 @@
 on;%0A
-import java.util.logging.Level;%0Aimport java.util.logging.Logger;%0A
 %0A%0Apu

update license header