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

com.haventec.nativeux.adapter.java.sdk.impl.utils.HttpUtils Maven / Gradle / Ivy

Go to download

Applications that would like to offer login pages with a native UX, could use this SDK to orchestrate the OpenID Connect flow from their application backend service.

The newest version!
package com.haventec.nativeux.adapter.java.sdk.impl.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.haventec.nativeux.adapter.java.sdk.api.exception.HaventecException;
import com.haventec.nativeux.adapter.java.sdk.api.exception.NativeUxAdapterSdkError;
import com.haventec.nativeux.adapter.java.sdk.api.models.DeleteUserRequest;
import com.haventec.nativeux.adapter.java.sdk.api.models.SelfServiceUpdateUserRequest;
import com.haventec.nativeux.adapter.java.sdk.api.models.UpdateUserRequest;
import com.haventec.nativeux.adapter.java.sdk.impl.model.OidcParams;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;

/**
 * Http Utils to create Http method request e.g. {@link HttpPatch}
 */
@Slf4j
@Builder
@Data
public class HttpUtils {

    private ObjectMapper objectMapper;

    /**
     * Create http Patch request with request object, url and host details.
     *
     * @param request request object e.g. {@link UpdateUserRequest}, {@link SelfServiceUpdateUserRequest}
     * @param url     url path to which API request e.g. /lp/api/oidc-landing-page/v1-0/user/
     * @param params  Haventec Connect params e.g. the params contains the hostname , nonce or htOidTxId
     * @param anUserJwt Authenticate User JWT e.g. eyJhbGciOiJSUzI1NiIsInR5ciwia2lkIiA6ICJFUVZ2ZWUxZGNydFYzMkRSMG00bHo5NTRPWjJ5djBWSkhjZTZFVG43VlNFIn0.eyJleHAiOjE2MzE2MTA5OTEsImlhdCI6MTYzMTU3NTAyNCwiYXV0aF90aW1lIjoxNjMxNTc0OTkxLCJqdGkiOiI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zpdTsJHtOcRvsLy9E90GW1gK2473Argq81ZYkmvOt7a45qLfWrZ_fEOxzK5SPaJkwNC7IGKuzjnhApoosheJ1-UwcTtIXL572SYKj-YAcmtLR6NioIgWziUO3ZQduKxh3OdVUtTjPSLBSTkCdk4BuovGBa9m
     * @return Http Patch request
     * @throws URISyntaxException URI Syntax Exception thrown e.g. when unable to build the URL
     * @throws HaventecException  Haventec Exception thrown e.g. when Object to Json conversion is not working
     */
    public HttpPatch createHttpPatch(Object request, String url, OidcParams params, String anUserJwt) throws URISyntaxException, HaventecException {
        String methodName = "createHttpPatch";
        StringBuilder urlBuilder = new StringBuilder();
        urlBuilder.append(params.getLandingPageHost()).append(url);
        URIBuilder builder = new URIBuilder(urlBuilder.toString());

        if (null != params.getNonce()) {
            builder.setParameter(Constants.AN_NONCE, params.getNonce());
        }
        if (null != params.getHtOidTxId()) {
            builder.setParameter(Constants.HT_OID_TX_ID, params.getHtOidTxId());
        }

        HttpPatch httpPatch = new HttpPatch(builder.build());

        try {
            ObjectWriter objectWriter = new ObjectMapper().writer().withDefaultPrettyPrinter();
            String jsonBody = objectWriter.writeValueAsString(request);
            httpPatch.setEntity(new StringEntity(jsonBody));
        } catch (JsonProcessingException | UnsupportedEncodingException e) {
            log.error("{} - error parsing request to json", methodName, e);
            throw new HaventecException(NativeUxAdapterSdkError.OBJECT_TO_JSON_BODY, e);
        }

        httpPatch.setHeader(Constants.ACCEPT, Constants.APPLICATION_JSON);
        httpPatch.setHeader(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON);


        if (anUserJwt != null) {
            httpPatch.setHeader(Constants.AUTHORIZATION, Constants.BEARER + anUserJwt);
        }

        return httpPatch;
    }

    /**
     * Create http delete request with request object, url and host details.
     *
     * @param request request object e.g. {@link DeleteUserRequest}
     * @param url     url path to which API request e.g. /lp/api/oidc-landing-page/v1-0/user/
     * @param params  Haventec Connect params e.g. the params contains the hostname , nonce or htOidTxId
     * @param anUserJwt Authenticate User JWT e.g. eyJhbGci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.emOQcMEayWATBmUXvf1cVPT6gDXrJvhDM_9WX7RzSDM5cUfdmQa8gZa_9yFhDC0BcdnYNdXnkdDaxFctWYlE1lmcSf_nmuN8Zhpnb3KRWPaudO5cuOrAE9diW68lmk8kWGqUf8rIOX6yjtWA82PSR04VGUNYdClN0H7a-w1QC_IM4gedH5X2wzpdTsJHtOcRvsLy9E90GW1gK2473Argq81ZYkmvOt7a45qLfWrZ_fEOxzK5SPaJkwNC7IGKuzjnhApoosheJ1-UwcTtIXL572SYKj-YAcmtLR6NioIgWziUO3ZQduKxh3OdVUtTjPSLBSTkCdk4BuovGBa9m
     * @return Http Delete request
     * @throws URISyntaxException URI Syntax Exception thrown e.g. when unable to build the URL
     * @throws HaventecException  Haventec Exception thrown e.g. when Object to Json conversion is not working
     */
    public HttpDeleteWithRequestBody createHttpDelete(DeleteUserRequest request, String url, OidcParams params, String anUserJwt) throws URISyntaxException, HaventecException {
        String methodName = "createHttpDelete";
        StringBuilder urlBuilder = new StringBuilder();
        urlBuilder.append(params.getLandingPageHost()).append(url);
        URIBuilder builder = new URIBuilder(urlBuilder.toString());

        if (null != params.getNonce()) {
            builder.setParameter(Constants.AN_NONCE, params.getNonce());
        }
        if (null != params.getHtOidTxId()) {
            builder.setParameter(Constants.HT_OID_TX_ID, params.getHtOidTxId());
        }

        HttpDeleteWithRequestBody httpDelete = new HttpDeleteWithRequestBody(builder.build());

        try {
            ObjectWriter objectWriter = new ObjectMapper().writer().withDefaultPrettyPrinter();
            String jsonBody = objectWriter.writeValueAsString(request);
            httpDelete.setEntity(new StringEntity(jsonBody));
        } catch (JsonProcessingException | UnsupportedEncodingException e) {
            log.error("{} - error parsing request to json", methodName, e);
            throw new HaventecException(NativeUxAdapterSdkError.OBJECT_TO_JSON_BODY, e);
        }

        httpDelete.setHeader(Constants.ACCEPT, Constants.APPLICATION_JSON);
        httpDelete.setHeader(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON);

        if (anUserJwt != null) {
            httpDelete.setHeader(Constants.AUTHORIZATION, Constants.BEARER + anUserJwt);
        }

        return httpDelete;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy