Example usage for org.apache.commons.net.util Base64 encodeBase64String

List of usage examples for org.apache.commons.net.util Base64 encodeBase64String

Introduction

In this page you can find the example usage for org.apache.commons.net.util Base64 encodeBase64String.

Prototype

public static String encodeBase64String(byte[] binaryData) 

Source Link

Document

Encodes binary data using the base64 algorithm into 76 character blocks separated by CRLF.

Usage

From source file:com.rim.logdriver.util.Grep.java

@Override
public int run(String[] args) throws Exception {
    Configuration conf = getConf(); // Configuration processed by ToolRunner
    // If run by Oozie, then load the Oozie conf too
    if (System.getProperty("oozie.action.conf.xml") != null) {
        conf.addResource(new URL("file://" + System.getProperty("oozie.action.conf.xml")));
    }/* w  w w .j  a v  a2  s  . co m*/

    FileSystem fs = FileSystem.get(conf);

    // The command line options
    String regex = null;
    List<Path> paths = new ArrayList<Path>();
    Path outputDir = null;

    // Load input files from the command line
    if (args.length < 3) {
        System.out.println("usage: [genericOptions] regex input [input ...] output");
        System.exit(1);
    }

    // Get the files we need from the command line.
    regex = args[0];
    for (int i = 1; i < args.length - 1; i++) {
        for (FileStatus f : fs.globStatus(new Path(args[i]))) {
            paths.add(f.getPath());
        }
    }
    outputDir = new Path(args[args.length - 1]);

    Job job = new Job(conf);
    Configuration jobConf = job.getConfiguration();

    job.setJarByClass(Grep.class);
    jobConf.setIfUnset("mapred.job.name", "Grep Files");

    // To propagate credentials within Oozie
    if (System.getenv("HADOOP_TOKEN_FILE_LOCATION") != null) {
        jobConf.set("mapreduce.job.credentials.binary", System.getenv("HADOOP_TOKEN_FILE_LOCATION"));
    }

    // Good output separators include things that are unsupported by XML. So we
    // just send the byte value of the character through. The restriction here
    // is that it can't be more than 1 byte when UTF-8 encoded, since it will be
    // read by Pig which only deals with single byte separators.
    {
        String outputSeparator = jobConf.get("logdriver.output.field.separator", DEFAULT_OUTPUT_SEPARATOR);
        byte[] bytes = outputSeparator.getBytes(UTF_8);
        if (bytes.length != 1) {
            LOG.error("The output separator must be a single byte in UTF-8.");
            return 1;
        }

        jobConf.set("logdriver.output.field.separator", Byte.toString(bytes[0]));
    }

    jobConf.set("logdriver.grep.regex", Base64.encodeBase64String(regex.getBytes("UTF-8")));

    job.setInputFormatClass(BoomInputFormat.class);
    job.setMapperClass(GrepMapper.class);
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(NullWritable.class);

    job.setNumReduceTasks(0);

    // And set the output as usual
    job.setOutputFormatClass(TextOutputFormat.class);
    TextOutputFormat.setOutputPath(job, outputDir);
    for (Path path : paths) {
        BoomInputFormat.addInputPath(job, path);
    }

    // Run the job.
    if (conf.getBoolean("job.wait", DEFAULT_WAIT_JOB)) {
        return job.waitForCompletion(true) ? 0 : 1;
    } else {
        job.submit();
        return 0;
    }

}

From source file:com.blackberry.logdriver.util.Grep.java

@Override
public int run(String[] args) throws Exception {
    Configuration conf = getConf(); // Configuration processed by ToolRunner
    // If run by Oozie, then load the Oozie conf too
    if (System.getProperty("oozie.action.conf.xml") != null) {
        conf.addResource(new URL("file://" + System.getProperty("oozie.action.conf.xml")));
    }/*w w w. ja  v a2 s .  c o m*/

    FileSystem fs = FileSystem.get(conf);

    // The command line options
    String regex = null;
    List<Path> paths = new ArrayList<Path>();
    Path outputDir = null;

    // Load input files from the command line
    if (args.length < 3) {
        System.out.println("usage: [genericOptions] regex input [input ...] output");
        System.exit(1);
    }

    // Get the files we need from the command line.
    regex = args[0];
    for (int i = 1; i < args.length - 1; i++) {
        for (FileStatus f : fs.globStatus(new Path(args[i]))) {
            paths.add(f.getPath());
        }
    }
    outputDir = new Path(args[args.length - 1]);

    @SuppressWarnings("deprecation")
    Job job = new Job(conf);
    Configuration jobConf = job.getConfiguration();

    job.setJarByClass(Grep.class);
    jobConf.setIfUnset("mapred.job.name", "Grep Files");

    // To propagate credentials within Oozie
    if (System.getenv("HADOOP_TOKEN_FILE_LOCATION") != null) {
        jobConf.set("mapreduce.job.credentials.binary", System.getenv("HADOOP_TOKEN_FILE_LOCATION"));
    }

    // Good output separators include things that are unsupported by XML. So we
    // just send the byte value of the character through. The restriction here
    // is that it can't be more than 1 byte when UTF-8 encoded, since it will be
    // read by Pig which only deals with single byte separators.
    {
        String outputSeparator = jobConf.get("logdriver.output.field.separator", DEFAULT_OUTPUT_SEPARATOR);
        byte[] bytes = outputSeparator.getBytes(UTF_8);
        if (bytes.length != 1) {
            LOG.error("The output separator must be a single byte in UTF-8.");
            return 1;
        }

        jobConf.set("logdriver.output.field.separator", Byte.toString(bytes[0]));
    }

    jobConf.set("logdriver.grep.regex", Base64.encodeBase64String(regex.getBytes("UTF-8")));

    job.setInputFormatClass(BoomInputFormat.class);
    job.setMapperClass(GrepMapper.class);
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(NullWritable.class);

    job.setNumReduceTasks(0);

    // And set the output as usual
    job.setOutputFormatClass(TextOutputFormat.class);
    TextOutputFormat.setOutputPath(job, outputDir);
    for (Path path : paths) {
        BoomInputFormat.addInputPath(job, path);
    }

    // Run the job.
    if (conf.getBoolean("job.wait", DEFAULT_WAIT_JOB)) {
        return job.waitForCompletion(true) ? 0 : 1;
    } else {
        job.submit();
        return 0;
    }

}

From source file:hd3gtv.mydmam.auth.AuthenticationBackend.java

/**
 * @return 12 first chars of Base64(SHA-264(random(1024b)))
 *///from  w  w  w . j a va 2 s  .co  m
public static String passwordGenerator() throws NoSuchAlgorithmException, NoSuchProviderException {
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    Random r = new Random();
    byte[] fill = new byte[1024];
    r.nextBytes(fill);
    byte[] key = md.digest(fill);
    return new String(Base64.encodeBase64String(key)).substring(0, 12);
}

From source file:eionet.webq.xforms.XFormsHTTPRequestAuthHandlerImpl.java

@Override
public void addAuthToHttpRequest(HttpRequestBase httpRequestBase, Map<Object, Object> context) {

    String uri = httpRequestBase.getURI().toString();

    String instance = null;/*w  w  w.j a  v a 2s . c o m*/
    String envelope = null;
    String requestURLHost = null;
    Integer fileId = null;
    String authentication = null;
    String sessionId = null;

    if (uri == null) {
        return;
    }

    // load bf context attributes
    if (context.get("instance") != null) {
        instance = (String) context.get("instance");
    }
    if (context.get("envelope") != null) {
        envelope = (String) context.get("envelope");
    }
    if (context.get(BF_REQUEST_URL_ATTRIBUTE) != null) {
        try {
            URI requestURI = new URI((String) context.get(BF_REQUEST_URL_ATTRIBUTE));
            requestURLHost = StringUtils.substringBefore(requestURI.toString(), requestURI.getHost())
                    + requestURI.getHost();
            LOGGER.info("bF requestURLHost= " + requestURLHost);
        } catch (URISyntaxException e) {
            LOGGER.warn("requestURL is not valid URL: " + context.get(BF_REQUEST_URL_ATTRIBUTE));
        }
    }
    if (context.get("fileId") != null) {
        fileId = Integer.valueOf((String) context.get("fileId"));
    }
    // http session attribute stored in betterform context
    if (context.get(BF_HTTP_SESSION_ATTRIBUTE) != null) {
        sessionId = (String) context.get(BF_HTTP_SESSION_ATTRIBUTE);
    }

    LOGGER.info("Get resource from XForm: " + uri);

    if (uri.startsWith(requestURLHost)) {
        // check if the request on the same (webq) host is done in the same session. Fix session id if required.
        if (sessionId != null) {
            validateSessionIdInRequestHeader(context, sessionId);
            LOGGER.info("Get resource from: " + uri);
        }
    } else {
        // add auth info only for URIs that are not on the same host.
        if (fileId != null && sessionId != null) {
            LOGGER.debug("Check if user is logged in to get resource for fileId=" + fileId);
            if (!context.containsKey(WEBQ_AUTH_ATTRIBUTE)) {
                // check if user is logged in - ask auth info from user_xml file table
                UserFile userFile = userFileService.getById(fileId);
                if (userFile.isAuthorized()) {
                    String authorizationInfo = userFile.getAuthorization();
                    String cookiesInfo = userFile.getCookies();
                    if (StringUtils.isNotEmpty(authorizationInfo)) {
                        authentication = "Authorization=" + authorizationInfo;
                    } else if (StringUtils.isNotEmpty(cookiesInfo)) {
                        authentication = "Cookie=" + cookiesInfo;
                    }
                }
                context.put(WEBQ_AUTH_ATTRIBUTE, authentication);
                LOGGER.debug("Store basic auth info in context for fileId=" + fileId);
            } else {
                // auth info stored in context
                authentication = context.get(WEBQ_AUTH_ATTRIBUTE) != null
                        ? (String) context.get(WEBQ_AUTH_ATTRIBUTE)
                        : null;
            }
            // add auth info only if user is logged in
            if (StringUtils.isNotEmpty(authentication)) {
                LOGGER.debug("User is logged in to get resource for fileId=" + fileId);

                String authAttribute = StringUtils.substringBefore(authentication, "=");

                // if the URI starts with instance or envelope URI, then we can use the basic auth retrieved from CDR.
                if (!"Cookie".equals(authAttribute)
                        && ((StringUtils.isNotBlank(instance) && uri.startsWith(instance))
                                || (StringUtils.isNotBlank(envelope) && uri.startsWith(envelope)))) {

                    String authAttributeValues = StringUtils.substringAfter(authentication, "=");
                    // prevent betterForm to overwrite cookies
                    /* Fall back to KnownHosts authorisation (qaaccount) if cookie auth mode is used.
                    // cookie mode does not work on test server.
                    if ("Cookie".equals(authAttribute)) {
                    if (context.containsKey(AbstractHTTPConnector.REQUEST_COOKIE)) {
                        context.remove(AbstractHTTPConnector.REQUEST_COOKIE);
                    }
                    }
                    */
                    httpRequestBase.addHeader(authAttribute, authAttributeValues);
                    LOGGER.info("Add " + authAttribute + " from session to URL: " + uri);
                } else {
                    // check if we have known host in db
                    KnownHost knownHost = knownHostsService.getKnownHost(uri);
                    if (knownHost != null) {
                        // add ticket parameter to request URI if needed
                        if (knownHost
                                .getAuthenticationMethod() == KnownHostAuthenticationMethod.REQUEST_PARAMETER) {
                            LOGGER.info("Add ticket parameter from known hosts to URL: " + uri);
                            uri = getUrlWithAuthParam(uri, knownHost);
                            if (!uri.equals(httpRequestBase.getURI().toString())) {
                                try {
                                    httpRequestBase.setURI(new URI(uri));
                                } catch (URISyntaxException e) {
                                    LOGGER.error("Unable to add known host ticket parameter for URI:" + uri);
                                    e.printStackTrace();
                                }
                            }
                        } else if (knownHost.getAuthenticationMethod() == KnownHostAuthenticationMethod.BASIC) {
                            // Add basic authorisation if needed
                            try {
                                LOGGER.info("Add basic auth from known hosts to URL: " + uri);
                                httpRequestBase.addHeader("Authorization", "Basic " + Base64.encodeBase64String(
                                        (knownHost.getKey() + ":" + knownHost.getTicket()).getBytes("utf-8"))
                                        .replaceAll("\n", ""));
                            } catch (UnsupportedEncodingException e) {
                                LOGGER.warn("UnsupportedEncodingException: utf-8");
                            }
                        }
                    }
                }
            }
        }
    }
}

From source file:com.rim.logdriver.util.FastSearch.java

@Override
public int run(String[] args) throws Exception {
    Configuration conf = getConf(); // Configuration processed by ToolRunner
    // If run by Oozie, then load the Oozie conf too
    if (System.getProperty("oozie.action.conf.xml") != null) {
        conf.addResource(new URL("file://" + System.getProperty("oozie.action.conf.xml")));
    }/* www  .ja v a 2 s . c om*/

    FileSystem fs = FileSystem.get(conf);

    // The command line options
    String searchString = null;
    List<Path> paths = new ArrayList<Path>();
    Path outputDir = null;

    // Load input files from the command line
    if (args.length < 3) {
        System.out.println("usage: [genericOptions] searchString input [input ...] output");
        System.exit(1);
    }

    // Get the files we need from the command line.
    searchString = args[0];
    for (int i = 1; i < args.length - 1; i++) {
        for (FileStatus f : fs.globStatus(new Path(args[i]))) {
            paths.add(f.getPath());
        }
    }
    outputDir = new Path(args[args.length - 1]);

    Job job = new Job(conf);
    Configuration jobConf = job.getConfiguration();

    job.setJarByClass(FastSearch.class);
    jobConf.setIfUnset("mapred.job.name", "Search Files");

    // To propagate credentials within Oozie
    if (System.getenv("HADOOP_TOKEN_FILE_LOCATION") != null) {
        jobConf.set("mapreduce.job.credentials.binary", System.getenv("HADOOP_TOKEN_FILE_LOCATION"));
    }

    // Good output separators include things that are unsupported by XML. So we
    // just send the byte value of the character through. The restriction here
    // is that it can't be more than 1 byte when UTF-8 encoded, since it will be
    // read by Pig which only deals with single byte separators.
    {
        String outputSeparator = jobConf.get("logdriver.output.field.separator", DEFAULT_OUTPUT_SEPARATOR);
        byte[] bytes = outputSeparator.getBytes(UTF_8);
        if (bytes.length != 1) {
            LOG.error("The output separator must be a single byte in UTF-8.");
            return 1;
        }

        jobConf.set("logdriver.output.field.separator", Byte.toString(bytes[0]));
    }

    jobConf.set("logdriver.search.string", Base64.encodeBase64String(searchString.getBytes("UTF-8")));

    job.setInputFormatClass(AvroBlockInputFormat.class);
    job.setMapperClass(SearchMapper.class);
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(NullWritable.class);

    job.setNumReduceTasks(0);

    // And set the output as usual
    job.setOutputFormatClass(TextOutputFormat.class);
    TextOutputFormat.setOutputPath(job, outputDir);
    for (Path path : paths) {
        AvroBlockInputFormat.addInputPath(job, path);
    }

    // Run the job.
    if (conf.getBoolean("job.wait", DEFAULT_WAIT_JOB)) {
        return job.waitForCompletion(true) ? 0 : 1;
    } else {
        job.submit();
        return 0;
    }
}

From source file:com.blackberry.logdriver.util.FastSearch.java

@Override
public int run(String[] args) throws Exception {
    Configuration conf = getConf(); // Configuration processed by ToolRunner
    // If run by Oozie, then load the Oozie conf too
    if (System.getProperty("oozie.action.conf.xml") != null) {
        conf.addResource(new URL("file://" + System.getProperty("oozie.action.conf.xml")));
    }/*from  www  . ja v a 2s.c om*/

    FileSystem fs = FileSystem.get(conf);

    // The command line options
    String searchString = null;
    List<Path> paths = new ArrayList<Path>();
    Path outputDir = null;

    // Load input files from the command line
    if (args.length < 3) {
        System.out.println("usage: [genericOptions] searchString input [input ...] output");
        System.exit(1);
    }

    // Get the files we need from the command line.
    searchString = args[0];
    for (int i = 1; i < args.length - 1; i++) {
        for (FileStatus f : fs.globStatus(new Path(args[i]))) {
            paths.add(f.getPath());
        }
    }
    outputDir = new Path(args[args.length - 1]);

    @SuppressWarnings("deprecation")
    Job job = new Job(conf);
    Configuration jobConf = job.getConfiguration();

    job.setJarByClass(FastSearch.class);
    jobConf.setIfUnset("mapred.job.name", "Search Files");

    // To propagate credentials within Oozie
    if (System.getenv("HADOOP_TOKEN_FILE_LOCATION") != null) {
        jobConf.set("mapreduce.job.credentials.binary", System.getenv("HADOOP_TOKEN_FILE_LOCATION"));
    }

    // Good output separators include things that are unsupported by XML. So we
    // just send the byte value of the character through. The restriction here
    // is that it can't be more than 1 byte when UTF-8 encoded, since it will be
    // read by Pig which only deals with single byte separators.
    {
        String outputSeparator = jobConf.get("logdriver.output.field.separator", DEFAULT_OUTPUT_SEPARATOR);
        byte[] bytes = outputSeparator.getBytes(UTF_8);
        if (bytes.length != 1) {
            LOG.error("The output separator must be a single byte in UTF-8.");
            return 1;
        }

        jobConf.set("logdriver.output.field.separator", Byte.toString(bytes[0]));
    }

    jobConf.set("logdriver.search.string", Base64.encodeBase64String(searchString.getBytes("UTF-8")));

    job.setInputFormatClass(AvroBlockInputFormat.class);
    job.setMapperClass(SearchMapper.class);
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(NullWritable.class);

    job.setNumReduceTasks(0);

    // And set the output as usual
    job.setOutputFormatClass(TextOutputFormat.class);
    TextOutputFormat.setOutputPath(job, outputDir);
    for (Path path : paths) {
        AvroBlockInputFormat.addInputPath(job, path);
    }

    // Run the job.
    if (conf.getBoolean("job.wait", DEFAULT_WAIT_JOB)) {
        return job.waitForCompletion(true) ? 0 : 1;
    } else {
        job.submit();
        return 0;
    }
}

From source file:eionet.webq.web.controller.WebQProxyDelegation.java

/**
 * Create HttpHeader with basic authentication info.
 *
 * @param knownHost KnownHost object//from   w  w  w  .j ava2  s.  c  om
 * @return HttpHeader with authorization attribute
 */

private HttpHeaders getHttpHeaderWithBasicAuthentication(KnownHost knownHost) {
    HttpHeaders authorization = new HttpHeaders();
    try {
        authorization
                .add("Authorization",
                        "Basic " + Base64
                                .encodeBase64String(
                                        (knownHost.getKey() + ":" + knownHost.getTicket()).getBytes("utf-8"))
                                .trim());
    } catch (UnsupportedEncodingException e) {
        LOGGER.warn("UnsupportedEncodingException: utf-8");
    }
    return authorization;
}

From source file:com.t_oster.visicut.misc.Helper.java

public static String getEncodedCredentials(String user, String password) {
    String result = "";

    if (user != null && !user.isEmpty() && password != null && !password.isEmpty()) {
        String credentials = user + ":" + password;

        try {/* w  ww .  java2  s . co  m*/
            result = Base64.encodeBase64String(credentials.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            result = Base64.encodeBase64String(credentials.getBytes());
        }

        // Remove line breaks in result, old versions of Base64.encodeBase64String add wrong line breaks
        result = result.replace("\n", "");
        result = result.replace("\r", "");
    }

    return result;
}

From source file:com.tremolosecurity.unison.openshiftv3.OpenShiftTarget.java

public String getAuthToken() throws Exception {
    HttpCon con = this.createClient();
    try {/*from  w w w . j a  v a2  s  . c om*/

        if (!this.useToken) {

            StringBuffer b = new StringBuffer();
            b.append(this.url)
                    .append("/oauth/authorize?response_type=token&client_id=openshift-challenging-client");
            HttpGet get = new HttpGet(b.toString());
            b.setLength(0);
            b.append(this.userName).append(':').append(this.password);
            String b64 = Base64.encodeBase64String(b.toString().getBytes("UTF-8"));
            b.setLength(0);
            b.append("Basic ").append(b64.substring(0, b64.length() - 2));
            get.addHeader(new BasicHeader("Authorization", b.toString()));

            HttpResponse resp = con.getHttp().execute(get);
            String token = "";
            if (resp.getStatusLine().getStatusCode() == 302) {
                String url = resp.getFirstHeader("Location").getValue();
                int start = url.indexOf("access_token") + "access_token=".length();
                int end = url.indexOf("&", start + 1);
                token = url.substring(start, end);

            } else {
                throw new Exception("Unable to obtain token : " + resp.getStatusLine().toString());
            }

            return token;
        } else {
            return this.osToken;
        }
    } finally {
        if (con != null) {
            con.getBcm().shutdown();
        }
    }
}

From source file:io.hops.hopsworks.common.util.HopsUtils.java

/**
 *
 * @param key// w w  w. j a  v a  2 s .co m
 * @param plaintext
 * @return
 * @throws Exception
 */
public static String encrypt(String key, String plaintext, String masterEncryptionPassword) throws Exception {

    Key aesKey = generateKey(key, masterEncryptionPassword);
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, aesKey);
    byte[] encrypted = cipher.doFinal(plaintext.getBytes());
    return Base64.encodeBase64String(encrypted);
}