All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hp.autonomy.hod.client.api.authentication.SignedRequest Maven / Gradle / Ivy

There is a newer version: 0.25.3
Show newest version
/*
 * Copyright 2015-2016 Hewlett-Packard Development Company, L.P.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
 */

package com.hp.autonomy.hod.client.api.authentication;

import com.hp.autonomy.hod.client.util.Hmac;
import com.hp.autonomy.hod.client.util.Request;
import lombok.Data;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Represents a request to HP Haven OnDemand which must be made from a user's browser. The token header should be set to
 * the value of the {@link #token} field. If there is a body, the Content-Type header should be set to "application/x-www-form-urlencoded".
 */
@Data
public class SignedRequest {
    private static final String UTF8 = "UTF-8";

    private final String url;
    private final Request.Verb verb;
    private final String body;
    private final String token;

    private SignedRequest(final String url, final Request.Verb verb, final String body, final String token) {
        this.url = url;
        this.verb = verb;
        this.body = body;
        this.token = token;
    }

    static SignedRequest sign(final Hmac hmac, final String endpoint, final AuthenticationToken token, final Request request) {
        final URIBuilder uriBuilder;

        try {
            uriBuilder = new URIBuilder(endpoint + request.getPath());
        } catch (final URISyntaxException e) {
            throw new IllegalArgumentException("Invalid endpoint or request path");
        }

        if (request.getQueryParameters() != null) {
            for (final Map.Entry> entry : request.getQueryParameters().entrySet()) {
                for (final String value : entry.getValue()) {
                    uriBuilder.addParameter(entry.getKey(), value);
                }
            }
        }

        final String bodyString;

        if (request.getBody() == null) {
            bodyString = null;
        } else {
            final List pairs = new LinkedList<>();

            for (final Map.Entry> entry : request.getBody().entrySet()) {
                for (final String value: entry.getValue()) {
                    pairs.add(new BasicNameValuePair(entry.getKey(), value));
                }
            }

            bodyString = URLEncodedUtils.format(pairs, UTF8);
        }

        final String tokenString = hmac.generateToken(request, token);
        return new SignedRequest(uriBuilder.toString(), request.getVerb(), bodyString, tokenString);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy