Example usage for org.apache.commons.codec.binary Base64 Base64

List of usage examples for org.apache.commons.codec.binary Base64 Base64

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary Base64 Base64.

Prototype

public Base64() 

Source Link

Document

Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.

Usage

From source file:de.scoopgmbh.copper.test.versioning.compatibility.TestJavaSerializer.java

private String serialize(final Object o) throws IOException {
    if (o == null)
        return null;
    final ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    final ObjectOutputStream oos = new ObjectOutputStream(baos);

    oos.writeObject(o);//from  w ww. j a v  a2s  . c om
    oos.close();
    baos.close();
    byte[] data = baos.toByteArray();
    final ByteArrayOutputStream baos2 = new ByteArrayOutputStream(1024);
    baos2.write(data, 0, 8);
    baos2.write(classNameReplacement.getBytes("UTF-8"));
    int offset = classNameReplacement.length() + 8;
    baos2.write(data, offset, data.length - offset);
    baos2.flush();
    baos2.close();
    data = baos2.toByteArray();
    boolean isCompressed = false;
    if (compress && compressThresholdSize <= data.length && data.length <= compressorMaxSize) {
        data = compressorTL.get().compress(data);
        isCompressed = true;
    }
    final String encoded = new Base64().encodeToString(data);
    final StringBuilder sb = new StringBuilder(encoded.length() + 4);
    sb.append(isCompressed ? 'C' : 'U').append(encoded);
    return sb.toString();
}

From source file:com.sixsq.slipstream.cookie.CryptoUtils.java

static private String savePublicKey(PublicKey publicKey) throws GeneralSecurityException {
    byte[] publicKeyBytes = publicKey.getEncoded();
    String publicKeyStr = new Base64().encodeToString(publicKeyBytes);
    return publicKeyStr;
}

From source file:com.claresco.tinman.servlet.XapiServletUtility.java

protected static String encodeBase64(String theSecret) {
    Base64 encoder = new Base64();

    return encoder.encodeAsString(theSecret.getBytes());
}

From source file:com.abm.mainet.water.ui.controller.PlumberLicenseFormController.java

@RequestMapping(method = RequestMethod.POST, params = "getCheckListAndCharges")
public ModelAndView doGetApplicableCheckListAndCharges(HttpServletRequest httpServletRequest) {
    this.bindModel(httpServletRequest);
    long orgId = UserSession.getCurrent().getOrganisation().getOrgid();
    ModelAndView modelAndView = null;//from  w ww.  j  a  v  a  2s .  com
    PlumberLicenseFormModel model = this.getModel();
    try {
        for (Iterator<Entry<Long, Set<File>>> it = FileUploadUtility.getCurrent().getFileMap().entrySet()
                .iterator(); it.hasNext();) {
            Entry<Long, Set<File>> entry = it.next();
            if (entry.getKey().longValue() == 0) {
                Set<File> set = entry.getValue();
                File file = set.iterator().next();
                String bytestring = "";
                Base64 base64 = new Base64();
                try {
                    bytestring = base64.encodeToString(FileUtils.readFileToByteArray(file));
                } catch (IOException e) {
                    logger.error("Exception has been occurred in file byte to string conversions", e);
                }
                String plumberImage = file.getName();
                model.setPlumberImage(plumberImage);
                model.getPlumberLicenseReqDTO().setPlumberImage(plumberImage);
                model.getPlumberLicenseReqDTO().setImageByteCode(bytestring);
                break;
            }
        }
        if (model.getPlumberImage() == null || model.getPlumberImage().isEmpty()) {
            this.getModel().addValidationError(ApplicationSession.getInstance()
                    .getMessage("water.plumberLicense.valMsg.uploadPlumberPhoto"));
        } else {
            model.findApplicableCheckListAndCharges(this.getModel().getServiceId(), orgId);
        }
        modelAndView = new ModelAndView("PlumberLicenseFormValidn", "command", model);
        if (model.getBindingResult() != null) {
            modelAndView.addObject(BindingResult.MODEL_KEY_PREFIX + MainetConstants.FORM_NAME,
                    getModel().getBindingResult());
        }
    } catch (Exception ex) {
        modelAndView = defaultExceptionFormView();
    }
    return modelAndView;
}

From source file:com.jamfsoftware.jss.healthcheck.controller.HTTPController.java

public int returnGETResponseCode(String URL) throws Exception {
    URL obj = new URL(URL);

    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    TrustModifier.relaxHostChecking(con);

    con.setRequestMethod("GET");
    con.setRequestProperty("User_Agent", USER_AGENT);
    Base64 b = new Base64();
    String encoding = b.encodeAsString((username + ":" + password).getBytes());
    con.setRequestProperty("Authorization", "Basic " + encoding);

    return con.getResponseCode();
}

From source file:davmail.smtp.TestSmtp.java

@Override
public void setUp() throws IOException {
    super.setUp();
    if (clientSocket == null) {
        // start gateway
        DavGateway.start();/*www.ja va 2 s  .c o m*/
        clientSocket = new Socket("localhost", Settings.getIntProperty("davmail.smtpPort"));
        socketOutputStream = new BufferedOutputStream(clientSocket.getOutputStream());
        socketInputStream = new BufferedInputStream(clientSocket.getInputStream());

        String banner = readLine();
        assertNotNull(banner);
        //String credentials = (char) 0 + Settings.getProperty("davmail.username") + (char) 0 + Settings.getProperty("davmail.password");
        String credentials = Settings.getProperty("davmail.username") + (char) 0
                + Settings.getProperty("davmail.username") + (char) 0
                + Settings.getProperty("davmail.password");
        writeLine("AUTH PLAIN " + new String(new Base64().encode(credentials.getBytes())));
        assertEquals("235 OK Authenticated", readLine());
    }
    if (session == null) {
        session = ExchangeSessionFactory.getInstance(Settings.getProperty("davmail.username"),
                Settings.getProperty("davmail.password"));
    }
}

From source file:gov.nih.nci.cabig.open2caaers.exchange.ParticipantODMMessageProcessor.java

private void checkAuthentication(Exchange exchange) {

    // get the authorization header
    String authorizationString = (String) exchange.getIn().getHeader("Authorization");
    if (StringUtils.isBlank(authorizationString)) {
        log.error(String.format("------------------------\n%s\n, Body : %s\n------------------",
                "Missing Authorization header", String.valueOf(exchange.getIn().getBody())));
        throw new RuntimeExpressionException("No Authentication found");
    }/*from   ww w.  ja  v  a  2 s. co  m*/

    try {
        String[] authInfoSplit = authorizationString.split("\\s+");

        // decode and validate the authorization header
        Base64 decoder = new Base64();
        byte[] decodedAuth = decoder.decode(authInfoSplit[1].getBytes());

        String usernamePassword = new String(decodedAuth, "UTF-8");
        String[] credentials = StringUtils.split(usernamePassword, ':');
        String username = credentials != null && credentials.length > 0 ? credentials[0] : null;
        String password = credentials != null && credentials.length > 1 ? credentials[1] : null;
        if (!StringUtils.equals(username, this.caaersWSUser)) {
            log.error(String.format(
                    "------------------------\n%s\nUsername : %s,\n Body : %s\n------------------",
                    "Invalid username ", String.valueOf(username), String.valueOf(exchange.getIn().getBody())));
            throw new RuntimeExpressionException("Invalid Authentication");
        }

        if (!StringUtils.equals(password, this.caaersWSPassword)) {
            log.error(String.format(
                    "------------------------\n%s\nPassword : %s,\n Body : %s\n------------------",
                    "Invalid password ", String.valueOf(password), String.valueOf(exchange.getIn().getBody())));
            throw new RuntimeExpressionException("Invalid Authentication");
        }
    } catch (UnsupportedEncodingException e) {
        log.error(String.format("------------------------\n%s\n, Body : %s\n------------------",
                "Unable to validate Authorization information", String.valueOf(exchange.getIn().getBody())), e);
        throw new RuntimeExpressionException("Invalid Authentication");
    }

}

From source file:edu.tsinghua.lumaqq.qq.net.HttpProxy.java

/**
 * ??/* w  ww .ja  v a 2 s. c  o  m*/
 * 
 * @param u ??
 * @param p ?
 * @throws IOException
 */
public HttpProxy(IProxyHandler handler, String u, String p) throws IOException {
    this(handler);
    username = u;
    password = p;
    if (u == null || "".equals(u.trim()) || p == null)
        authParam = null;
    else {
        Base64 codec = new Base64();
        authParam = new String(codec.encode((u + ":" + p).getBytes())).getBytes();
    }
}

From source file:com.cloudant.mazha.HttpRequests.java

public HttpRequests(CouchConfig config) {
    this.httpClient = createHttpClient(config);
    this.jsonHelper = new JSONHelper();
    String username = config.getUsername();
    String password = config.getPassword();

    if (!Strings.isNullOrEmpty(username) && !Strings.isNullOrEmpty(password)) {
        String authString = username + ":" + password;
        Base64 base64 = new Base64();
        authHeaderValue = "Basic " + new String(base64.encode(authString.getBytes()));
    }//from w w  w . j  a v a  2s.c o  m

}

From source file:mx.bigdata.sat.cfdi.TFDv1.java

public int verificar() throws Exception {
    if (tfd == null) {
        return 601; //No contiene timbrado
    }//from  w  ww. j  ava  2 s  .c  om
    Base64 b64 = new Base64();
    String sigStr = tfd.getSelloSAT();
    byte[] signature = b64.decode(sigStr);
    byte[] bytes = getOriginalBytes();
    Signature sig = Signature.getInstance("SHA1withRSA");
    sig.initVerify(cert);
    sig.update(bytes);
    boolean verified = sig.verify(signature);
    return verified ? 600 : 602; //Sello del timbrado no valido
}