Java tutorial
/* Copyright (c) 2010 Ministry of the Interior and Kingdom Relations, * the Netherlands. All rights reserved. * * This file is part of the MinBZK Search Enricher indexing generator. * * Search Enricher 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. * * Search Enricher 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 Search Enricher. If not, see <http://www.gnu.org/licenses/>. */ package nl.minbzk.dwr.zoeken.enricher.uploader; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URI; import java.net.URLEncoder; import java.util.Arrays; import java.util.List; import nl.minbzk.dwr.zoeken.enricher.GeneratorResult; import nl.minbzk.dwr.zoeken.enricher.Uploader; import nl.minbzk.dwr.zoeken.enricher.generator.ACIGeneratorResult; import nl.minbzk.dwr.zoeken.enricher.settings.EnricherJob; import nl.minbzk.dwr.zoeken.enricher.settings.EnricherSettings; import nl.minbzk.dwr.zoeken.enricher.settings.EnricherSettings.GeneratorType; import org.apache.commons.lang.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; /** * ACI autn envelope uploader. * * @author Jasper van Veghel <j.veghel@rijksoverheid.nl> */ public class ACIResultUploader implements Uploader { /** * The logger. */ private static Logger logger = LoggerFactory.getLogger(ACIResultUploader.class); /** * Constants. */ private static final int HTTP_STATUS_SUCCESS = 200; private static final String HEADER_USER_AGENT = "User-Agent"; private static final String HEADER_CONTENT_TYPE = "Content-Type"; private static final String HEADER_CONTENT_TYPE_ENCODED = "application/x-www-form-urlencoded"; /** * The HTTP client. */ private final HttpClient httpClient = new DefaultHttpClient(); /** * The add URI. */ private final String addUri; /** * The add URI. */ private final String commitUri; /** * The delete reference URI. */ private final String deleteReferenceUri; /** * The delete DocId URI. */ private final String deleteDocIdUri; /** * The base encoding. */ private final String baseEncoding; /** * The user agent. */ private final String userAgent; /** * Default constructor. * * @param settings */ @Autowired public ACIResultUploader(final EnricherSettings settings) { addUri = settings.getAciAddUri(); deleteReferenceUri = settings.getAciDeleteReferenceUri(); deleteDocIdUri = settings.getAciDeleteDocIdUri(); commitUri = settings.getAciCommitUri(); baseEncoding = settings.getEncoding(); userAgent = settings.getAciUserAgent(); } /** * {@inheritDoc} */ @Override public void upload(final EnricherJob job, final GeneratorResult result) throws Exception { String file = ((ACIGeneratorResult) result).generateFile(job.getProcessedPath(), baseEncoding); HttpPost httpPost = new HttpPost(new URI(addUri + "?" + URLEncoder.encode(file, baseEncoding))); httpPost.setHeader(HEADER_USER_AGENT, userAgent); httpPost.setHeader(HEADER_CONTENT_TYPE, HEADER_CONTENT_TYPE_ENCODED); HttpResponse response = httpClient.execute(httpPost, new ResponseHandler<HttpResponse>() { @Override public HttpResponse handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { return response; } }); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream inputStream = entity.getContent(); try { if (logger.isDebugEnabled()) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); logger.debug("Received entity response from server for result path: " + reader.readLine()); } } catch (IOException e) { // No connection to release - simply rethrow the exception throw e; } catch (RuntimeException e) { // Abort the HTTP request in order to release the connection httpPost.abort(); throw e; } finally { // Trigger a connection release inputStream.close(); } } if (response.getStatusLine().getStatusCode() != HTTP_STATUS_SUCCESS) throw new Exception("Generated result could not be uploaded because of status " + response.getStatusLine().getStatusCode() + " (" + response.getStatusLine().getReasonPhrase() + ")"); else if (logger.isDebugEnabled()) logger.debug("Result path " + file + " sent to the indexer"); } /** * {@inheritDoc} */ @Override public void commit(final EnricherJob job) throws Exception { HttpPost httpPost = new HttpPost(new URI(commitUri)); httpPost.setHeader(HEADER_USER_AGENT, userAgent); HttpResponse response = httpClient.execute(httpPost, new ResponseHandler<HttpResponse>() { @Override public HttpResponse handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { return response; } }); if (response.getStatusLine().getStatusCode() != HTTP_STATUS_SUCCESS) throw new Exception("Generated result could not be committed because of status " + response.getStatusLine().getStatusCode() + " (" + response.getStatusLine().getReasonPhrase() + ")"); else if (logger.isDebugEnabled()) logger.debug("Results have been committed to the indexer"); } /** * {@inheritDoc} */ @Override public void deleteByDocId(final EnricherJob job, final String[] documents) throws Exception { HttpPost httpPost = new HttpPost(new URI(deleteDocIdUri + "?dredbname=" + job.getDatabaseName() + "&docs=" + URLEncoder.encode(StringUtils.join(documents, " "), baseEncoding))); httpPost.setHeader(HEADER_USER_AGENT, userAgent); httpPost.setHeader(HEADER_CONTENT_TYPE, HEADER_CONTENT_TYPE_ENCODED); HttpResponse response = httpClient.execute(httpPost, new ResponseHandler<HttpResponse>() { @Override public HttpResponse handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { return response; } }); if (response.getStatusLine().getStatusCode() != HTTP_STATUS_SUCCESS) throw new Exception("Given document(s) could not be deleted because of status " + response.getStatusLine().getStatusCode() + " (" + response.getStatusLine().getReasonPhrase() + ")"); else if (logger.isDebugEnabled()) logger.debug("Result document(s) delete request sent to the indexer"); } /** * {@inheritDoc} */ @Override public void deleteByReference(final EnricherJob job, final String field, final String[] documents) throws Exception { HttpPost httpPost = new HttpPost( new URI(deleteReferenceUri + "?dredbname=" + job.getDatabaseName() + "&field=" + field + "&docs=" + URLEncoder.encode(StringUtils.join(documents, " "), baseEncoding))); httpPost.setHeader(HEADER_USER_AGENT, userAgent); httpPost.setHeader(HEADER_CONTENT_TYPE, HEADER_CONTENT_TYPE_ENCODED); HttpResponse response = httpClient.execute(httpPost, new ResponseHandler<HttpResponse>() { @Override public HttpResponse handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { return response; } }); if (response.getStatusLine().getStatusCode() != HTTP_STATUS_SUCCESS) throw new Exception("Given document(s) could not be deleted because of status " + response.getStatusLine().getStatusCode() + " (" + response.getStatusLine().getReasonPhrase() + ")"); else if (logger.isDebugEnabled()) logger.debug("Result document(s) delete request sent to the indexer"); } /** * {@inheritDoc} */ @Override public List<GeneratorType> getType() { return Arrays.asList(new GeneratorType[] { GeneratorType.ACI }); } /** * {@inheritDoc} */ @Override public void writeOut(final String databaseName, final PrintWriter writer) { writer.write(String.format("ACI database %s", databaseName)); } }