Java tutorial
/* * This work was created by participants in the DataONE project, and is * jointly copyrighted by participating institutions in DataONE. For * more information on DataONE, see our web site at http://dataone.org. * * Copyright 2014 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package org.dataone.proto.trove.mn.http.client; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.net.HttpURLConnection; import org.apache.http.HttpEntity; import org.apache.http.HttpException; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.log4j.Logger; import org.dataone.service.exceptions.AuthenticationTimeout; import org.dataone.service.exceptions.IdentifierNotUnique; import org.dataone.service.exceptions.InsufficientResources; import org.dataone.service.exceptions.InvalidCredentials; import org.dataone.service.exceptions.InvalidRequest; import org.dataone.service.exceptions.InvalidSystemMetadata; import org.dataone.service.exceptions.InvalidToken; import org.dataone.service.exceptions.NotAuthorized; import org.dataone.service.exceptions.NotFound; import org.dataone.service.exceptions.NotImplemented; import org.dataone.service.exceptions.ServiceFailure; import org.dataone.service.exceptions.UnsupportedMetadataType; import org.dataone.service.exceptions.UnsupportedType; import java.io.IOException; import java.io.InputStream; import java.security.Principal; import java.security.cert.X509Certificate; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; /** * * @author waltz */ public class DataHttpClientHandler { static Logger logger = Logger.getLogger(DataHttpClientHandler.class.getName()); // this can be set by caller if the default discovery mechanism is not applicable public DataHttpClientHandler() { } public static synchronized String executePost(HttpClient httpclient, HttpPost httpPost) throws ServiceFailure, NotFound, InvalidToken, NotAuthorized, IdentifierNotUnique, UnsupportedType, InsufficientResources, InvalidSystemMetadata, NotImplemented, InvalidCredentials, InvalidRequest, AuthenticationTimeout, UnsupportedMetadataType, HttpException { String response = null; try { HttpResponse httpResponse = httpclient.execute(httpPost); if (httpResponse.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) { HttpEntity resEntity = httpResponse.getEntity(); if (resEntity != null) { logger.debug("Response content length: " + resEntity.getContentLength()); logger.debug("Chunked?: " + resEntity.isChunked()); BufferedInputStream in = new BufferedInputStream(resEntity.getContent()); ByteArrayOutputStream resOutputStream = new ByteArrayOutputStream(); byte[] buff = new byte[32 * 1024]; int len; while ((len = in.read(buff)) > 0) { resOutputStream.write(buff, 0, len); } response = new String(resOutputStream.toByteArray()); logger.debug(response); resOutputStream.close(); in.close(); } } else { HttpExceptionHandler.deserializeAndThrowException(httpResponse); } } catch (IOException ex) { throw new ServiceFailure("1002", ex.getMessage()); } return response; } public static synchronized InputStream executeGet(HttpClient httpclient, HttpGet httpGet) throws ServiceFailure, NotFound, InvalidToken, NotAuthorized, IdentifierNotUnique, UnsupportedType, InsufficientResources, InvalidSystemMetadata, NotImplemented, InvalidCredentials, InvalidRequest, AuthenticationTimeout, UnsupportedMetadataType, HttpException { InputStream response = null; try { HttpResponse httpResponse = httpclient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) { HttpEntity resEntity = httpResponse.getEntity(); if (resEntity != null) { response = resEntity.getContent(); } else { throw new ServiceFailure("1002", "response code OK, but Entity not returned?" + httpResponse.toString()); } } else { HttpExceptionHandler.deserializeAndThrowException(httpResponse); } } catch (IOException ex) { throw new ServiceFailure("1002", ex.getMessage()); } return response; } /** * Show details of an X509 certificate, printing the information to STDOUT. * * @param cert the certificate to be displayed */ public void displayCertificate(X509Certificate cert) { if (cert == null) { return; } logger.debug("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); Principal issuerDN = cert.getIssuerDN(); logger.debug(" Issuer: " + issuerDN.toString()); Date notBefore = cert.getNotBefore(); DateFormat fmt = SimpleDateFormat.getDateTimeInstance(); logger.debug(" From: " + fmt.format(notBefore)); Date notAfter = cert.getNotAfter(); logger.debug(" To: " + fmt.format(notAfter)); Principal subjectDN = cert.getSubjectDN(); logger.debug("Subject: " + subjectDN.toString()); logger.debug("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); } }