Java tutorial
/** * Copyright (C) 2014 - Swisscom (Schweiz) AG * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see http://www.gnu.org/licenses/. * * @author <a href="mailto:benjamin.mantei@swisscom.com">Benjamin Mantei</a> */ package com.pingidentity.adapters.idp.mobileid.restservice; import java.io.IOException; import java.security.SecureRandom; import java.security.cert.X509Certificate; import javax.net.ssl.SSLContext; import javax.security.auth.x500.X500PrivateCredential; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import com.pingidentity.adapters.idp.mobileid.util.TlsConnection; public class MssRequestHandlerRest { // private static final Logger log = // Logger.getLogger(MssRequestHandler.class); private final String mssServiceUrl; private final SSLContext sslContext; private final CloseableHttpClient httpClient; private MssRequestHandlerRest(Builder builder) { TlsConnection connection = new TlsConnection(); sslContext = connection.createClientAuthenticatedConnection(builder.clientKey, builder.clientKeyPwd, builder.serverCertificate); mssServiceUrl = builder.serverUrl; httpClient = HttpClients.custom().setSslcontext(sslContext).build(); } public MssSignatureResponseJson sendSignatureRequest(MssSignatureRequestJson signatureRequest) { try { HttpPost httpRequest = new HttpPost(mssServiceUrl); httpRequest.addHeader("Content-Type", "application/json;charset=UTF-8"); httpRequest.addHeader("Accept", "application/json"); httpRequest.setEntity(new StringEntity(signatureRequest.getRequestMessage())); CloseableHttpResponse httpResponse = httpClient.execute(httpRequest); String responseMessage = EntityUtils.toString(httpResponse.getEntity()); return new MssSignatureResponseJson.Builder().responseMessage(responseMessage).build(); } catch (IOException e) { throw new RuntimeException("Failed to send the request or to receive the response"); } } /** * Creates a random transaction id beginning with 'pf' * * @param digits * number of digits without 'pf' * @return the generated transaction id */ public static String createTransId(int digits) { final String VALUES = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; SecureRandom rand = new SecureRandom(); rand.setSeed(System.currentTimeMillis()); StringBuffer randBuffer = new StringBuffer("pf"); for (int i = 0; i < digits; i++) { randBuffer.append(VALUES.charAt(rand.nextInt(VALUES.length()))); } return randBuffer.toString(); } public static class Builder { private X509Certificate serverCertificate; private X500PrivateCredential clientKey; private String clientKeyPwd; private String serverUrl; public Builder mssServerURL(String baseUrl, String port) { this.serverUrl = baseUrl + port; return this; } public Builder mssServerURL(String baseUrl) { this.serverUrl = baseUrl; return this; } public Builder clientSslKey(X500PrivateCredential clientKey, String clientKeyPwd) { this.clientKey = clientKey; this.clientKeyPwd = clientKeyPwd; return this; } public Builder mssServerCertificate(X509Certificate serverCertificate) { this.serverCertificate = serverCertificate; return this; } public MssRequestHandlerRest build() { return new MssRequestHandlerRest(this); } } }