Example usage for javax.xml.bind DatatypeConverter printBase64Binary

List of usage examples for javax.xml.bind DatatypeConverter printBase64Binary

Introduction

In this page you can find the example usage for javax.xml.bind DatatypeConverter printBase64Binary.

Prototype

public static String printBase64Binary(byte[] val) 

Source Link

Document

Converts an array of bytes into a string.

Usage

From source file:de.dominicscheurer.passwords.SafePwdGen.java

/**
 * Converts an input to a Base64 encoded String value.
 * /*  w w w .  j  a va  2s. co m*/
 * @param input
 *            Input to encode.
 * @return Base64-encoded input value.
 */
private static String getBase64(String input) throws UnsupportedEncodingException {
    return DatatypeConverter.printBase64Binary(input.getBytes("UTF-8"));
}

From source file:com.ibm.streamsx.rest.StreamsConnection.java

/**
 * Connection to IBM Streams/*from   w ww.j  a v a 2  s .c om*/
 * 
 * @param userName
 *            String representing the userName to connect to the instance
 * @param authToken
 *            String representing the password to connect to the instance
 * @param url
 *            String representing the root url to the REST API, for example:
 *            https:server:port/streams/rest
 */
protected StreamsConnection(String userName, String authToken, String url) {
    this.userName = userName;
    String apiCredentials = userName + ":" + authToken;
    apiKey = "Basic " + DatatypeConverter.printBase64Binary(apiCredentials.getBytes(StandardCharsets.UTF_8));

    executor = Executor.newInstance();
    setStreamsRESTURL(url);
}

From source file:com.github.stephanarts.cas.ticket.registry.provider.AddMethodTest.java

@Test
public void testValidInput() throws Exception {
    final byte[] serializedTicket;
    final HashMap<Integer, Ticket> map = new HashMap<Integer, Ticket>();
    final JSONObject params = new JSONObject();
    final IMethod method = new AddMethod(map);

    final String ticketId = "ST-1234567890ABCDEFGHIJKL-crud";
    final ServiceTicket ticket = mock(ServiceTicket.class, withSettings().serializable());
    when(ticket.getId()).thenReturn(ticketId);

    try {/*from   ww w  .  j a va 2s.  c o  m*/
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream so = new ObjectOutputStream(bo);
        so.writeObject(ticket);
        so.flush();
        serializedTicket = bo.toByteArray();

        params.put("ticket-id", ticketId);
        params.put("ticket", DatatypeConverter.printBase64Binary(serializedTicket));
        method.execute(params);

    } catch (final JSONRPCException e) {
        Assert.fail(e.getMessage());
    } catch (final Exception e) {
        throw new Exception(e);
    }
}

From source file:com.github.stephanarts.cas.ticket.registry.provider.UpdateMethodTest.java

@Test
public void testValidInput() throws Exception {
    final byte[] serializedTicket;
    final HashMap<Integer, Ticket> map = new HashMap<Integer, Ticket>();
    final JSONObject params = new JSONObject();
    final IMethod method = new UpdateMethod(map);

    final String ticketId = "ST-1234567890ABCDEFGHIJKL-crud";
    final ServiceTicket ticket = mock(ServiceTicket.class, withSettings().serializable());
    when(ticket.getId()).thenReturn(ticketId);

    map.put(ticketId.hashCode(), ticket);

    try {/*from   w w  w  .  j  a  va  2s.  c  o m*/
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream so = new ObjectOutputStream(bo);
        so.writeObject(ticket);
        so.flush();
        serializedTicket = bo.toByteArray();

        params.put("ticket-id", ticketId);
        params.put("ticket", DatatypeConverter.printBase64Binary(serializedTicket));
        method.execute(params);

    } catch (final JSONRPCException e) {
        Assert.fail(e.getMessage());
    } catch (final Exception e) {
        throw new Exception(e);
    }
}

From source file:com.ibm.streamsx.topology.internal.streaminganalytics.RestUtils.java

public static String getAPIKey(JsonObject credentials) {
    String userid = jstring(credentials, "userid");
    String password = jstring(credentials, "password");

    String api_creds = userid + ":" + password;
    String apiKey = "Basic " + DatatypeConverter.printBase64Binary(api_creds.getBytes(StandardCharsets.UTF_8));
    return apiKey;
}

From source file:com.github.stephanarts.cas.ticket.registry.provider.GetTicketsMethod.java

/**
 * Execute the JSONRPCFunction.//from   w  w w  .j  a  v  a  2 s . c  om
 *
 * @param params    JSONRPC Method Parameters.
 *
 * @return          JSONRPC result object
 *
 * @throws JSONRPCException implementors can throw JSONRPCExceptions containing the error.
 */
public JSONObject execute(final JSONObject params) throws JSONRPCException {
    JSONObject result = new JSONObject();
    JSONArray tickets = new JSONArray();

    String ticketId = null;

    if (params.length() != 0) {
        throw new JSONRPCException(-32602, "Invalid Params");
    }

    for (Ticket ticket : this.map.values()) {

        byte[] serializedTicketArray = { 0 };

        try {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream so = new ObjectOutputStream(bo);
            so.writeObject(ticket);
            so.flush();
            serializedTicketArray = bo.toByteArray();
        } catch (final Exception e) {
            logger.debug(e.getMessage());
            throw new JSONRPCException(-32500, "Error extracting Ticket");
        }

        tickets.put(DatatypeConverter.printBase64Binary(serializedTicketArray));
    }

    logger.debug("GetTickets: " + tickets.length());

    result.put("tickets", tickets);

    return result;
}

From source file:com.denimgroup.threadfix.importer.impl.remoteprovider.utils.RemoteProviderHttpUtilsImpl.java

@Override
@Nonnull//  w  w  w.j  a  va2  s.c  o m
public HttpResponse getUrl(String url, final String username, final String password) {
    assert url != null;

    return getUrlWithConfigurer(url, new RequestConfigurer() {
        @Override
        public void configure(HttpMethodBase method) {
            if (username != null && password != null) {
                String login = username + ":" + password;
                String encodedLogin = DatatypeConverter.printBase64Binary(login.getBytes());
                method.setRequestHeader("Authorization", "Basic " + encodedLogin);
            }
            method.setRequestHeader("Content-type", "text/xml; charset=UTF-8");
        }
    });
}

From source file:com.github.stephanarts.cas.ticket.registry.provider.GetMethod.java

/**
 * Execute the JSONRPCFunction./*from  w  w  w.j ava  2  s.  c  om*/
 *
 * @param params    JSONRPC Method Parameters.
 *
 * @return          JSONRPC result object
 *
 * @throws JSONRPCException implementors can throw JSONRPCExceptions containing the error.
 */
public JSONObject execute(final JSONObject params) throws JSONRPCException {
    JSONObject result = new JSONObject();
    Ticket ticket;

    logger.debug("GET");

    String ticketId = null;

    if (params.length() != 1) {
        throw new JSONRPCException(-32602, "Invalid Params");
    }
    if (!(params.has("ticket-id"))) {
        throw new JSONRPCException(-32602, "Invalid Params");
    }

    ticketId = params.getString("ticket-id");

    ticket = this.map.get(ticketId.hashCode());

    if (ticket == null) {
        throw new JSONRPCException(-32503, "Missing Ticket");
    }

    byte[] serializedTicketArray = { 0 };

    try {
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream so = new ObjectOutputStream(bo);
        so.writeObject(ticket);
        so.flush();
        serializedTicketArray = bo.toByteArray();
    } catch (final Exception e) {
        logger.debug(e.getMessage());
        throw new JSONRPCException(-32500, "Error extracting Ticket");
    }

    result.put("ticket-id", ticketId);
    result.put("ticket", DatatypeConverter.printBase64Binary(serializedTicketArray));
    return result;
}

From source file:ch.rasc.wampspring.cra.DefaultAuthenticationHandler.java

public static String generateHMacSHA256(final String key, final String data)
        throws InvalidKeyException, NoSuchAlgorithmException {
    Assert.notNull(key, "key is required");
    Assert.notNull(data, "data is required");

    final Mac hMacSHA256 = Mac.getInstance("HmacSHA256");
    byte[] hmacKeyBytes = key.getBytes(StandardCharsets.UTF_8);
    final SecretKeySpec secretKey = new SecretKeySpec(hmacKeyBytes, "HmacSHA256");
    hMacSHA256.init(secretKey);// w w w.  ja  v  a 2s  . co  m
    byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
    byte[] res = hMacSHA256.doFinal(dataBytes);

    return DatatypeConverter.printBase64Binary(res);
}

From source file:com.webpagebytes.wpbsample.utility.NotificationUtility.java

public static void fetchReportImages(WPBContentProvider contentProvider, WPBModel model,
        Map<String, String> content) {
    ByteArrayOutputStream baos_users_fo = new ByteArrayOutputStream(10000);
    ByteArrayOutputStream baos_transactions_fo = new ByteArrayOutputStream(10000);
    ByteArrayOutputStream baos_deposits_fo = new ByteArrayOutputStream(10000);
    ByteArrayOutputStream baos_withdrawals_fo = new ByteArrayOutputStream(10000);

    ByteArrayOutputStream baos_users = new ByteArrayOutputStream(10000);
    ByteArrayOutputStream baos_transactions = new ByteArrayOutputStream(10000);
    ByteArrayOutputStream baos_deposits = new ByteArrayOutputStream(10000);
    ByteArrayOutputStream baos_withdrawals = new ByteArrayOutputStream(10000);

    String fopTransactionsGuid = model.getCmsModel().get(WPBModel.GLOBALS_KEY)
            .get("imageReportTransactionsGuid");
    String fopUsersGuid = model.getCmsModel().get(WPBModel.GLOBALS_KEY).get("imageReportUsersGuid");
    String fopDepositsGuid = model.getCmsModel().get(WPBModel.GLOBALS_KEY).get("imageReportDepositsGuid");
    String fopWithdrawalsGuid = model.getCmsModel().get(WPBModel.GLOBALS_KEY).get("imageReportWithdrawalsGuid");

    contentProvider.writePageContent(fopUsersGuid, model, baos_users_fo);
    model.getCmsApplicationModel().put("type", "tx");
    contentProvider.writePageContent(fopTransactionsGuid, model, baos_transactions_fo);
    model.getCmsApplicationModel().put("type", "d");
    contentProvider.writePageContent(fopDepositsGuid, model, baos_deposits_fo);
    model.getCmsApplicationModel().put("type", "w");
    contentProvider.writePageContent(fopWithdrawalsGuid, model, baos_withdrawals_fo);

    ByteArrayInputStream bis_users = new ByteArrayInputStream(baos_users_fo.toByteArray());
    ;/* w w w. j  a va2 s  .c o m*/
    ByteArrayInputStream bis_transactions = new ByteArrayInputStream(baos_transactions_fo.toByteArray());
    ;
    ByteArrayInputStream bis_deposits = new ByteArrayInputStream(baos_deposits_fo.toByteArray());
    ;
    ByteArrayInputStream bis_withdrawals = new ByteArrayInputStream(baos_withdrawals_fo.toByteArray());
    ;

    try {
        SampleFopService fopService = SampleFopService.getInstance();

        fopService.getContent(bis_users, MimeConstants.MIME_PNG, baos_users);
        fopService.getContent(bis_transactions, MimeConstants.MIME_PNG, baos_transactions);
        fopService.getContent(bis_deposits, MimeConstants.MIME_PNG, baos_deposits);
        fopService.getContent(bis_withdrawals, MimeConstants.MIME_PNG, baos_withdrawals);

        String base64Users = DatatypeConverter.printBase64Binary(baos_users.toByteArray());
        String base64Transactions = DatatypeConverter.printBase64Binary(baos_transactions.toByteArray());
        String base64Deposits = DatatypeConverter.printBase64Binary(baos_deposits.toByteArray());
        String base64Withdrawals = DatatypeConverter.printBase64Binary(baos_withdrawals.toByteArray());

        content.put(CONTENT_IMG_USERS, base64Users);
        content.put(CONTENT_IMG_TRANSACTIONS, base64Transactions);
        content.put(CONTENT_IMG_DEPOSITS, base64Deposits);
        content.put(CONTENT_IMG_WITHDRAWALS, base64Withdrawals);
    } catch (Exception e) {
        e.printStackTrace(System.out);
    } finally {
        IOUtils.closeQuietly(baos_users_fo);
        IOUtils.closeQuietly(baos_transactions_fo);
        IOUtils.closeQuietly(baos_deposits_fo);
        IOUtils.closeQuietly(baos_withdrawals_fo);
        IOUtils.closeQuietly(baos_users);
        IOUtils.closeQuietly(baos_transactions);
        IOUtils.closeQuietly(baos_deposits);
        IOUtils.closeQuietly(baos_withdrawals);
    }

}