Example usage for com.amazonaws.auth EnvironmentVariableCredentialsProvider EnvironmentVariableCredentialsProvider

List of usage examples for com.amazonaws.auth EnvironmentVariableCredentialsProvider EnvironmentVariableCredentialsProvider

Introduction

In this page you can find the example usage for com.amazonaws.auth EnvironmentVariableCredentialsProvider EnvironmentVariableCredentialsProvider.

Prototype

EnvironmentVariableCredentialsProvider

Source Link

Usage

From source file:org.springfield.lou.servlet.LouServlet.java

License:Open Source License

private String handleFileUpload(HttpServletRequest request) {
    System.out.println("HANDLE FILE UPLOAD");
    try {/*from w  ww.ja  va  2  s  .  c  o m*/
        String targetid = request.getParameter("targetid");
        System.out.println("TARGETID UPLOAD=" + targetid);
        String screenid = request.getParameter("screenid");
        String cfilename = request.getParameter("cfilename");
        System.out.println("CFILENAME=" + cfilename);
        String cfilesize = request.getParameter("cfilesize");
        System.out.println("CFILESIZE=" + cfilesize);

        Html5ApplicationInterface app = null;
        String url = request.getRequestURI();
        int pos = url.indexOf("/domain/");
        if (pos != -1) {
            String tappname = url.substring(pos);
            app = ApplicationManager.instance().getApplication(tappname);
        }
        Screen eventscreen = app.getScreen(screenid);

        if (eventscreen == null)
            return null;

        String method = eventscreen.getModel()
                .getProperty("/screen['upload']/target['" + targetid + "']/method");
        System.out.println("METHOD=" + method);

        String destpath = eventscreen.getModel()
                .getProperty("/screen['upload']/target['" + targetid + "']/destpath");
        System.out.println("DESTPATH=" + destpath + " T=" + targetid);
        if (destpath == null || destpath.equals("")) {
            setUploadError(eventscreen, targetid, "destpath not set");
            return null;
        }

        String destname_prefix = eventscreen.getModel()
                .getProperty("/screen['upload']/target['" + targetid + "']/destname_prefix");
        if (destname_prefix == null || destname_prefix.equals("")) {
            setUploadError(eventscreen, targetid, "destname_prefix not set");
            return null;
        }

        String filetype = eventscreen.getModel()
                .getProperty("/screen['upload']/target['" + targetid + "']/filetype");
        if (filetype == null || filetype.equals("")) {
            setUploadError(eventscreen, targetid, "filetype not set");
            return null;
        }

        String fileext = eventscreen.getModel()
                .getProperty("/screen['upload']/target['" + targetid + "']/fileext");
        if (fileext == null || fileext.equals("")) {
            setUploadError(eventscreen, targetid, "fileext not set");
            return null;
        }

        String checkupload = eventscreen.getModel()
                .getProperty("/screen['upload']/target['" + targetid + "']/checkupload");
        if (checkupload == null || checkupload.equals("")) {
            setUploadError(eventscreen, targetid, "checkupload not set");
            return null;
        }

        String storagehost = eventscreen.getModel()
                .getProperty("/screen['upload']/target['" + targetid + "']/storagehost");
        if (storagehost == null || storagehost.equals("")) {
            setUploadError(eventscreen, targetid, "storagehost not set");
            return null;
        }

        String destname_type = eventscreen.getModel()
                .getProperty("/screen['upload']/target['" + targetid + "']/destname_type");
        if (destname_type == null || destname_type.equals("")) {
            setUploadError(eventscreen, targetid, "destname_type not set");
            return null;
        }

        String publicpath = eventscreen.getModel()
                .getProperty("/screen['upload']/target['" + targetid + "']/publicpath");
        if (publicpath == null || publicpath.equals("")) {
            setUploadError(eventscreen, targetid, "publicpath not set");
            return null;
        }

        // here we can check if its a valid upload based on filename and other specs and kill if needed, also map real extension 

        fileext = getValidExtension(fileext, cfilename);
        if (fileext == null)
            return null; // kill the request its not a valid format

        if (method.equals("s3amazon")) {
            String bucketname = eventscreen.getModel()
                    .getProperty("/screen['upload']/target['" + targetid + "']/bucketname");
            if (bucketname == null || bucketname.equals("")) {
                setUploadError(eventscreen, targetid, "bucketname not set");
                return null;
            }

            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withCredentials(new EnvironmentVariableCredentialsProvider()).build();
            String filename = "unknown";
            int storageport = 22;

            if (destname_type.equals("epoch")) {
                filename = destpath + destname_prefix + "" + new Date().getTime();
            }

            String publicurl = publicpath + bucketname + "/" + filename + "." + fileext;

            FsPropertySet ps = new FsPropertySet(); // we will use this to send status reports back
            ps.setProperty("action", "start");
            ps.setProperty("progress", "0");
            ps.setProperty("cfilename", cfilename);
            ps.setProperty("url", publicurl);
            eventscreen.getModel().setProperties("/screen/upload/" + targetid, ps);

            try {
                InputStream inst = request.getInputStream();
                int read = 0;
                int readtotal = 0;
                int b;
                while ((b = inst.read()) != 44) {
                    // skip the base64 tagline, not sure how todo this better
                }

                Base64InputStream b64i = new Base64InputStream(inst);

                //System.out.println("Uploading a new object to S3 from a stream "+bucketname+"/"+filename+"."+fileext);

                ObjectMetadata metadata = new ObjectMetadata();
                metadata.setContentType(filetype + "/" + fileext);

                PutObjectRequest or = new PutObjectRequest(bucketname, filename + "." + fileext, b64i,
                        metadata);

                or.setGeneralProgressListener(new UploadProgressListener(eventscreen.getModel(), publicurl,
                        cfilename, cfilesize, targetid));
                s3Client.putObject(or);

            } catch (AmazonServiceException ase) {
                ase.printStackTrace();
            }
            ps.setProperty("action", "done");
            ps.setProperty("progress", "100");
            ps.setProperty("cfilename", cfilename);
            ps.setProperty("url", publicurl);

            eventscreen.getModel().setProperties("/screen/upload/" + targetid, ps);
            return bucketname + "/" + filename + "." + fileext;

        } else if (method.equals("scp")) {
            String pemfile = eventscreen.getModel()
                    .getProperty("/screen['upload']/target['" + targetid + "']/pemfile");
            if (destpath == null || destpath.equals("")) {
                setUploadError(eventscreen, targetid, "destpath not set");
                return null;
            }

            String storagename = eventscreen.getModel()
                    .getProperty("/screen['upload']/target['" + targetid + "']/storagename");
            if (storagename == null || storagehost.equals("")) {
                setUploadError(eventscreen, targetid, "storagename not set");
                return null;
            }

            String filename = "unknown";
            int storageport = 22;

            if (destname_type.equals("epoch")) {
                filename = destname_prefix + "" + new Date().getTime();
            }

            String publicurl = publicpath + filename + "." + fileext;

            FsPropertySet ps = new FsPropertySet(); // we will use this to send status reports back
            ps.setProperty("action", "start");
            ps.setProperty("progress", "0");
            ps.setProperty("url", publicurl);
            eventscreen.getModel().setProperties("/screen/upload/" + targetid, ps);

            JSch jsch = new JSch();
            jsch.addIdentity(pemfile);
            jsch.setConfig("StrictHostKeyChecking", "no");
            Session session = jsch.getSession(storagename, storagehost, storageport);
            session.connect();
            Channel channel = session.openChannel("sftp");

            channel.connect();
            ChannelSftp channelSftp = (ChannelSftp) channel;
            channelSftp.cd(destpath);

            InputStream inst = request.getInputStream();
            int read = 0;
            int readtotal = 0;
            int b;
            while ((b = inst.read()) != 44) {
                // skip the base64 tagline, not sure how todo this better
            }
            Base64InputStream b64i = new Base64InputStream(inst);

            channelSftp.put(b64i, filename + "." + fileext);

            ps.setProperty("action", "done");
            ps.setProperty("progress", "100");
            ps.setProperty("url", publicurl);
            eventscreen.getModel().setProperties("/screen/upload/" + targetid, ps);
            return filename + "." + fileext;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.xmlsh.aws.gradle.AwsPluginExtension.java

License:BSD License

public AWSCredentialsProvider newCredentialsProvider(String profileName) {
    return new AWSCredentialsProviderChain(new EnvironmentVariableCredentialsProvider(),
            new SystemPropertiesCredentialsProvider(),
            Strings.isNullOrEmpty(profileName) == false ? new ProfileCredentialsProvider(profileName) : EMPTY,
            new ProfileCredentialsProvider(this.profileName), new InstanceProfileCredentialsProvider());
}

From source file:uk.co.jassoft.email.EmailSenderService.java

public void send(String recipientAddress, String subject, Map emailContent) throws EmailSendException {
    try {//  w w w .  j a  va2  s. c  om
        if (System.getenv("API_SEND_EMAILS").equals("false")) {
            LOG.info("API_SEND_EMAILS is set to [{}]", System.getenv("API_SEND_EMAILS"));
            return;
        }

        // Construct an object to contain the recipient address.
        Destination destination = new Destination().withToAddresses(recipientAddress);

        // Create the subject and body of the message.
        Content subjectContent = new Content().withData(subject);

        Body body = new Body();

        if (template != null)
            body = body.withText(new Content().withData(VelocityEngineUtils
                    .mergeTemplateIntoString(velocityEngine, template, "UTF-8", emailContent)));

        if (htmlTemplate != null)
            body = body.withHtml(new Content().withData(VelocityEngineUtils
                    .mergeTemplateIntoString(velocityEngine, htmlTemplate, "UTF-8", emailContent)));

        // Create a message with the specified subject and body.
        Message message = new Message().withSubject(subjectContent).withBody(body);

        // Assemble the email.
        SendEmailRequest request = new SendEmailRequest().withSource(fromAddress).withDestination(destination)
                .withMessage(message);

        AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(
                new EnvironmentVariableCredentialsProvider());
        Region REGION = Region.getRegion(Regions.EU_WEST_1);
        client.setRegion(REGION);

        // Send the email.
        client.sendEmail(request);
    } catch (Exception exception) {
        throw new EmailSendException(exception.getMessage(), exception);
    }
}