+++ /dev/null
-/*
- * Copyright IBM Corporation, 2013. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.controller.commons.httpclient;\r
-\r
-import java.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-\r
-import org.apache.http.Header;\r
-import org.apache.http.HeaderIterator;\r
-import org.apache.http.HttpEntity;\r
-import org.apache.http.client.ClientProtocolException;\r
-import org.apache.http.client.config.RequestConfig;\r
-import org.apache.http.client.methods.CloseableHttpResponse;\r
-import org.apache.http.client.methods.HttpDelete;\r
-import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;\r
-import org.apache.http.client.methods.HttpGet;\r
-import org.apache.http.client.methods.HttpPost;\r
-import org.apache.http.client.methods.HttpPut;\r
-import org.apache.http.client.methods.HttpRequestBase;\r
-import org.apache.http.entity.StringEntity;\r
-import org.apache.http.impl.client.CloseableHttpClient;\r
-import org.apache.http.impl.client.HttpClients;\r
-import org.apache.http.util.EntityUtils;\r
-\r
-public class HTTPClient {\r
- static public HTTPResponse sendRequest(HTTPRequest request) throws Exception {\r
-\r
- CloseableHttpClient httpclient = HttpClients.createDefault();\r
- if (httpclient == null)\r
- throw new ClientProtocolException("Couldn't create an HTTP client");\r
- RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(request.getTimeout())\r
- .setConnectTimeout(request.getTimeout()).build();\r
- HttpRequestBase httprequest;\r
- String method = request.getMethod();\r
-\r
- if (method.equalsIgnoreCase("GET")) {\r
- httprequest = new HttpGet(request.getUri());\r
- } else if (method.equalsIgnoreCase("POST")) {\r
- httprequest = new HttpPost(request.getUri());\r
- if (request.getEntity() != null) {\r
- StringEntity sentEntity = new StringEntity(request.getEntity());\r
- sentEntity.setContentType(request.getContentType());\r
- ((HttpEntityEnclosingRequestBase) httprequest).setEntity(sentEntity);\r
- }\r
- } else if (method.equalsIgnoreCase("PUT")) {\r
- httprequest = new HttpPut(request.getUri());\r
- if (request.getEntity() != null) {\r
- StringEntity sentEntity = new StringEntity(request.getEntity());\r
- sentEntity.setContentType(request.getContentType());\r
- ((HttpEntityEnclosingRequestBase) httprequest).setEntity(sentEntity);\r
- }\r
- } else if (method.equalsIgnoreCase("DELETE")) {\r
- httprequest = new HttpDelete(request.getUri());\r
- } else {\r
- httpclient.close();\r
- throw new IllegalArgumentException("This profile class only supports GET, POST, PUT, and DELETE methods");\r
- }\r
- httprequest.setConfig(requestConfig);\r
-\r
- // add request headers\r
- Iterator<String> headerIterator = request.getHeaders().keySet().iterator();\r
- while (headerIterator.hasNext()) {\r
- String header = headerIterator.next();\r
- Iterator<String> valueIterator = request.getHeaders().get(header).iterator();\r
- while (valueIterator.hasNext()) {\r
- httprequest.addHeader(header, valueIterator.next());\r
- }\r
- }\r
-\r
- CloseableHttpResponse response = httpclient.execute(httprequest);\r
- try {\r
- HttpEntity receivedEntity = response.getEntity();\r
- int httpResponseCode = response.getStatusLine().getStatusCode();\r
- HTTPResponse ans = new HTTPResponse();\r
- HashMap<String, List<String>> headerMap = new HashMap<String, List<String>>();\r
-\r
- // copy response headers\r
- HeaderIterator it = response.headerIterator();\r
- while (it.hasNext()) {\r
- Header h = it.nextHeader();\r
- String name = h.getName();\r
- String value = h.getValue();\r
- if (headerMap.containsKey(name))\r
- headerMap.get(name).add(value);\r
- else {\r
- List<String> list = new ArrayList<String>();\r
- list.add(value);\r
- headerMap.put(name, list);\r
- }\r
- }\r
- ans.setHeaders(headerMap);\r
-\r
- if (httpResponseCode > 299) {\r
- ans.setStatus(httpResponseCode);\r
- ans.setEntity(response.getStatusLine().getReasonPhrase());\r
- return ans;\r
- }\r
- ans.setStatus(response.getStatusLine().getStatusCode());\r
- if (receivedEntity != null)\r
- ans.setEntity(EntityUtils.toString(receivedEntity));\r
- else\r
- ans.setEntity(null);\r
- return ans;\r
- } finally {\r
- response.close();\r
- }\r
- }\r
-}\r