de.fischer.thotti.core.runner.TargetURLGenerator.java Source code

Java tutorial

Introduction

Here is the source code for de.fischer.thotti.core.runner.TargetURLGenerator.java

Source

/*
 * Copyright 2011 Oliver B. Fischer
 *
 * 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 de.fischer.thotti.core.runner;

import de.fischer.thotti.awscommon.AWSAccessCredentials;
import de.fischer.thotti.awscommon.util.SignedURLBuilder;
import de.fischer.thotti.awscommon.util.ValidityPeriod;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import java.util.UUID;

public class TargetURLGenerator extends AWSAccessCredentials {

    public static final void main(String[] args) {
        String accessKey = null;
        String secretKey = null;

        Options options = createCommandLineOption();

        CommandLineParser parser = new GnuParser();
        try {
            // parse the command line arguments
            CommandLine line = parser.parse(options, args);

            if (!line.hasOption("ak")) {
                throw new ParseException("AWS Accress Key is missing.");
            }

            if (!line.hasOption("sk")) {
                throw new ParseException("AWS Secret Key is missing.");
            }

            accessKey = line.getOptionValue("ak");
            secretKey = line.getOptionValue("sk");

        } catch (ParseException exp) {
            System.err.println("Parsing failed.  Reason: " + exp.getMessage());
            System.exit(1);
        }

        TargetURLGenerator generator = new TargetURLGenerator();

        generator.setAccessKey(accessKey);
        generator.setSecretKey(secretKey);

        generator.run();
    }

    private void run() {
        String key = UUID.randomUUID().toString() + ".xml";

        // @todo: Make this configurable!
        String url = new SignedURLBuilder().withCredentials(this).withBucket("thotti-results")
                .withS3Endpoint("http://s3.amazonaws.com").withHTTPVerb(SignedURLBuilder.HTTPVerb.PUT)
                .withObject(key).withMimeType("text/xml").withExpirationIn(ValidityPeriod.ONE_WEEK).build();

        System.out.println(url);
    }

    private static Options createCommandLineOption() {
        Options options = new Options();

        Option accessKey = OptionBuilder.withArgName("AWS Access Key").hasArg().create("ak");

        Option secretKey = OptionBuilder.withArgName("AWS Secret Key").hasArg().create("sk");

        options.addOption(accessKey);
        options.addOption(secretKey);

        return options;
    }
}