Java tutorial
/** * Copyright 2010 Vstra Gtalandsregionen * * This library is free software; you can redistribute it and/or modify * it under the terms of version 2.1 of the GNU Lesser General Public * License as published by the Free Software Foundation. * * This library 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * */ package se.vgregion.pubsub.push.impl; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import nu.xom.Document; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.HttpConnectionParams; public class HttpUtil { public static DefaultHttpClient getClient() { DefaultHttpClient client = new DefaultHttpClient(); //ConnManagerParams.setMaxTotalConnections(client.getParams(), 100); HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); HttpConnectionParams.setSoTimeout(client.getParams(), 10000); return client; } public static boolean successStatus(HttpResponse response) { int status = response.getStatusLine().getStatusCode(); return status >= 200 && status < 300; } public static String readContent(HttpEntity entity) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); entity.writeTo(out); return out.toString("UTF-8"); } public static HttpEntity createEntity(String s) { try { return new StringEntity(s, "UTF-8"); } catch (UnsupportedEncodingException shouldNeverHappen) { throw new RuntimeException(shouldNeverHappen); } } public static HttpEntity createEntity(Document doc) { try { return new StringEntity(doc.toXML(), "UTF-8"); } catch (UnsupportedEncodingException shouldNeverHappen) { throw new RuntimeException(shouldNeverHappen); } } public static List<String> getContentTypes(HttpResponse response) { Header[] contentTypes = response.getHeaders("Content-Type"); List<String> contentTypesStr = new ArrayList<String>(); for (Header contentType : contentTypes) { contentTypesStr.add(contentType.getValue()); } return contentTypesStr; } public static void closeQuitely(HttpResponse response) { if (response != null && response.getEntity() != null) { try { response.getEntity().getContent().close(); } catch (IOException ignore) { } catch (IllegalStateException ignore) { } } } }