Example usage for java.util Base64 getDecoder

List of usage examples for java.util Base64 getDecoder

Introduction

In this page you can find the example usage for java.util Base64 getDecoder.

Prototype

public static Decoder getDecoder() 

Source Link

Document

Returns a Decoder that decodes using the Basic type base64 encoding scheme.

Usage

From source file:xtremweb.dispatcher.HTTPHandler.java

/**
 * This retrieves the user from login/password inside Authorization header
 *
 * @since 10.2.0/*from w  w  w  .  j ava2s.c  o m*/
 */
private UserInterface userFromBasicAuth(final HttpServletRequest request) throws IOException {
    final HttpSession session = request.getSession(true);

    String login = null;
    String password = null;
    ;

    final String authorization = request.getHeader("Authorization");
    if (authorization != null && authorization.startsWith("Basic")) {
        // Authorization: Basic base64credentials
        String base64Credentials = authorization.substring("Basic".length()).trim();
        String credentials = new String(Base64.getDecoder().decode(base64Credentials),
                Charset.forName("UTF-8"));
        // credentials = username:password
        final String[] values = credentials.split(":", 2);

        if (values != null && values.length > 1) {
            login = values[0];
            password = values[1];
        }

        if ((login == null) || (password == null)) {
            return null;
        }

        try {
            final UserInterface ret = DBInterface.getInstance()
                    .user(SQLRequest.MAINTABLEALIAS + "." + UserInterface.Columns.LOGIN.toString() + "='"
                            + login + "' AND " + SQLRequest.MAINTABLEALIAS + "."
                            + UserInterface.Columns.PASSWORD.toString() + "='" + password + "'");

            session.setAttribute(XWPostParams.XWLOGIN.toString(), login);
            session.setAttribute(XWPostParams.XWPASSWD.toString(), password);
            return ret;
        } catch (final Exception e) {
            throw new IOException("userFromPostParams error : " + e.getMessage());
        }
    }
    return null;
}

From source file:org.apache.pulsar.functions.worker.rest.api.ComponentImpl.java

public String triggerFunction(final String tenant, final String namespace, final String functionName,
        final String input, final InputStream uploadedInputStream, final String topic) {

    if (!isWorkerServiceAvailable()) {
        throwUnavailableException();/* w w w  . ja v a2 s. c  o  m*/
    }

    // validate parameters
    try {
        validateTriggerRequestParams(tenant, namespace, functionName, topic, input, uploadedInputStream);
    } catch (IllegalArgumentException e) {
        log.error("Invalid trigger function request @ /{}/{}/{}", tenant, namespace, functionName, e);
        throw new RestException(Status.BAD_REQUEST, e.getMessage());
    }

    FunctionMetaDataManager functionMetaDataManager = worker().getFunctionMetaDataManager();
    if (!functionMetaDataManager.containsFunction(tenant, namespace, functionName)) {
        log.error("Function in trigger function does not exist @ /{}/{}/{}", tenant, namespace, functionName);
        throw new RestException(Status.NOT_FOUND, String.format("Function %s doesn't exist", functionName));
    }

    FunctionMetaData functionMetaData = functionMetaDataManager.getFunctionMetaData(tenant, namespace,
            functionName);

    String inputTopicToWrite;
    if (topic != null) {
        inputTopicToWrite = topic;
    } else if (functionMetaData.getFunctionDetails().getSource().getInputSpecsCount() == 1) {
        inputTopicToWrite = functionMetaData.getFunctionDetails().getSource().getInputSpecsMap().keySet()
                .iterator().next();
    } else {
        log.error("Function in trigger function has more than 1 input topics @ /{}/{}/{}", tenant, namespace,
                functionName);
        throw new RestException(Status.BAD_REQUEST,
                "Function in trigger function has more than 1 input topics");
    }
    if (functionMetaData.getFunctionDetails().getSource().getInputSpecsCount() == 0 || !functionMetaData
            .getFunctionDetails().getSource().getInputSpecsMap().containsKey(inputTopicToWrite)) {
        log.error("Function in trigger function has unidentified topic @ /{}/{}/{} {}", tenant, namespace,
                functionName, inputTopicToWrite);
        throw new RestException(Status.BAD_REQUEST, "Function in trigger function has unidentified topic");
    }
    try {
        worker().getBrokerAdmin().topics().getSubscriptions(inputTopicToWrite);
    } catch (PulsarAdminException e) {
        log.error("Function in trigger function is not ready @ /{}/{}/{}", tenant, namespace, functionName);
        throw new RestException(Status.BAD_REQUEST, "Function in trigger function is not ready");
    }
    String outputTopic = functionMetaData.getFunctionDetails().getSink().getTopic();
    Reader<byte[]> reader = null;
    Producer<byte[]> producer = null;
    try {
        if (outputTopic != null && !outputTopic.isEmpty()) {
            reader = worker().getClient().newReader().topic(outputTopic).startMessageId(MessageId.latest)
                    .create();
        }
        producer = worker().getClient().newProducer(Schema.AUTO_PRODUCE_BYTES()).topic(inputTopicToWrite)
                .create();
        byte[] targetArray;
        if (uploadedInputStream != null) {
            targetArray = new byte[uploadedInputStream.available()];
            uploadedInputStream.read(targetArray);
        } else {
            targetArray = input.getBytes();
        }
        MessageId msgId = producer.send(targetArray);
        if (reader == null) {
            return null;
        }
        long curTime = System.currentTimeMillis();
        long maxTime = curTime + 1000;
        while (curTime < maxTime) {
            Message msg = reader.readNext(10000, TimeUnit.MILLISECONDS);
            if (msg == null)
                break;
            if (msg.getProperties().containsKey("__pfn_input_msg_id__")
                    && msg.getProperties().containsKey("__pfn_input_topic__")) {
                MessageId newMsgId = MessageId.fromByteArray(
                        Base64.getDecoder().decode((String) msg.getProperties().get("__pfn_input_msg_id__")));

                if (msgId.equals(newMsgId) && msg.getProperties().get("__pfn_input_topic__")
                        .equals(TopicName.get(inputTopicToWrite).toString())) {
                    return new String(msg.getData());
                }
            }
            curTime = System.currentTimeMillis();
        }
        throw new RestException(Status.REQUEST_TIMEOUT, "Request Timed Out");
    } catch (IOException e) {
        throw new RestException(Status.INTERNAL_SERVER_ERROR, e.getMessage());
    } finally {
        if (reader != null) {
            reader.closeAsync();
        }
        if (producer != null) {
            producer.closeAsync();
        }
    }
}