Java tutorial
/* * The MIT License (MIT) * * Copyright (c) 2014, Groupon, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.groupon.jenkins.util; import com.fasterxml.jackson.databind.ObjectMapper; import hudson.ProxyConfiguration; import jenkins.model.Jenkins; import org.apache.commons.httpclient.URIException; import org.apache.commons.io.IOUtils; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.conn.params.ConnRoutePNames; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import java.io.IOException; import java.net.InetSocketAddress; import java.net.Proxy; import java.util.Map; public class HttpPoster { public String post(String url, Map postData) throws IOException { DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost post = new HttpPost(url); try { post.setEntity(new StringEntity(new ObjectMapper().writeValueAsString(postData))); HttpResponse response = httpclient.execute(post); HttpHost proxy = getProxy(post); if (proxy != null) { httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException(response.getStatusLine().toString()); } return getResponse(response); } finally { httpclient.getConnectionManager().shutdown(); } } private String getResponse(HttpResponse response) throws IOException { return response.getEntity() == null || response.getEntity().getContent() == null ? null : IOUtils.toString(response.getEntity().getContent()); } private HttpHost getProxy(HttpUriRequest method) throws URIException { ProxyConfiguration proxy = Jenkins.getInstance().proxy; if (proxy == null) return null; Proxy p = proxy.createProxy(method.getURI().getHost()); switch (p.type()) { case DIRECT: return null; case HTTP: InetSocketAddress sa = (InetSocketAddress) p.address(); return new HttpHost(sa.getHostName(), sa.getPort()); case SOCKS: default: return null; } } }