Java tutorial
/* * Copyright (C) 2012 McEvoy Software Ltd * * 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/>. * */ package com.ettrema.httpclient; import com.bradmcevoy.http.exceptions.BadRequestException; import com.bradmcevoy.http.exceptions.ConflictException; import com.bradmcevoy.http.exceptions.NotAuthorizedException; import com.bradmcevoy.http.exceptions.NotFoundException; import java.io.*; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import org.apache.commons.io.IOUtils; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.protocol.HTTP; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @author mcevoyb */ public class Utils { private static final Logger log = LoggerFactory.getLogger(Utils.class); /** * Execute the given request, populate any returned content to the outputstream, * and return the status code * * @param client * @param m * @param out - may be null * @return * @throws IOException * */ public static int executeHttpWithStatus(HttpClient client, HttpUriRequest m, OutputStream out) throws IOException { HttpResult result = executeHttpWithResult(client, m, out); return result.getStatusCode(); } public static HttpResult executeHttpWithResult(HttpClient client, HttpUriRequest m, OutputStream out) throws IOException { HttpResponse resp = client.execute(m); HttpEntity entity = resp.getEntity(); if (entity != null) { InputStream in = null; try { in = entity.getContent(); if (out != null) { IOUtils.copy(in, out); } } finally { IOUtils.closeQuietly(in); } } Map<String, String> mapOfHeaders = new HashMap<String, String>(); for (Header h : resp.getAllHeaders()) { mapOfHeaders.put(h.getName(), h.getValue()); // TODO: should concatenate multi-valued headers } HttpResult result = new HttpResult(resp.getStatusLine().getStatusCode(), mapOfHeaders); return result; } public static void close(InputStream in) { try { if (in == null) { return; } in.close(); } catch (IOException ex) { log.warn("Exception closing stream: " + ex.getMessage()); } } public static void close(OutputStream out) { try { if (out == null) { return; } out.close(); } catch (IOException ex) { log.warn("Exception closing stream: " + ex.getMessage()); } } public static long write(InputStream in, OutputStream out, final ProgressListener listener) throws IOException { long bytes = 0; byte[] arr = new byte[1024]; int s = in.read(arr); bytes += s; try { while (s >= 0) { if (listener != null && listener.isCancelled()) { throw new CancelledException(); } out.write(arr, 0, s); s = in.read(arr); bytes += s; if (listener != null) { listener.onProgress(bytes, null, null); } } } catch (IOException e) { throw e; } catch (Throwable e) { log.error("exception copying bytes", e); throw new RuntimeException(e); } return bytes; } /** * Wraps the outputstream in a bufferedoutputstream and writes to it * * the outputstream is closed and flushed before returning * * @param in * @param out * @param listener * @throws IOException */ public static long writeBuffered(InputStream in, OutputStream out, final ProgressListener listener) throws IOException { BufferedOutputStream bout = null; try { bout = new BufferedOutputStream(out); long bytes = Utils.write(in, out, listener); bout.flush(); out.flush(); return bytes; } finally { Utils.close(bout); Utils.close(out); } } public static void processResultCode(int result, String href) throws com.ettrema.httpclient.HttpException, NotAuthorizedException, ConflictException, BadRequestException, NotFoundException { if (result >= 200 && result < 300) { return; } else if (result >= 300 && result < 400) { switch (result) { case 301: throw new RedirectException(result, href); case 302: throw new RedirectException(result, href); case 304: break; default: throw new RedirectException(result, href); } } else if (result >= 400 && result < 500) { switch (result) { case 400: throw new BadRequestException(href); case 401: throw new NotAuthorizedException(href); case 403: throw new NotAuthorizedException(href); case 404: throw new NotFoundException(href); case 405: throw new MethodNotAllowedException(result, href); case 409: throw new ConflictException(href); default: throw new GenericHttpException(result, href); } } else if (result >= 500 && result < 600) { throw new InternalServerError(href, result); } else { throw new GenericHttpException(result, href); } } public static class CancelledException extends IOException { } public static String format(Map<String, String> parameters, final String encoding) { final StringBuilder result = new StringBuilder(); for (Entry<String, String> p : parameters.entrySet()) { final String encodedName = encode(p.getKey(), encoding); final String value = p.getValue(); final String encodedValue = value != null ? encode(value, encoding) : ""; if (result.length() > 0) { result.append("&"); } result.append(encodedName); result.append("="); result.append(encodedValue); } return result.toString(); } // private static String decode (final String content, final String encoding) { // try { // return URLDecoder.decode(content, // encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET); // } catch (UnsupportedEncodingException problem) { // throw new IllegalArgumentException(problem); // } // } private static String encode(final String content, final String encoding) { try { return URLEncoder.encode(content, encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET); } catch (UnsupportedEncodingException problem) { throw new IllegalArgumentException(problem); } } }