2 * Copyright (c) 2014 NEC Corporation
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this
7 * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.vtn.app.run.config.rest.parser;
12 import java.net.ConnectException;
14 import java.util.Map.Entry;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 import org.json.JSONException;
20 import org.json.JSONObject;
22 import com.sun.jersey.api.client.Client;
23 import com.sun.jersey.api.client.ClientResponse;
24 import com.sun.jersey.api.client.ClientHandlerException;
25 import com.sun.jersey.api.client.WebResource;
26 import com.sun.jersey.api.client.WebResource.Builder;
27 import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
28 import org.opendaylight.vtn.app.run.config.rest.client.CRUDOperation;
29 import org.opendaylight.vtn.app.run.config.rest.client.VTNClientException;
30 import org.opendaylight.vtn.app.run.config.rest.enumgroups.APPLICATION_TYPE;
31 import org.opendaylight.vtn.app.run.config.rest.enumgroups.HTTP_RESPONSE;
33 public class CRUDImpl implements CRUDOperation {
38 static final Logger LOG = LoggerFactory.getLogger(CRUDImpl.class);
40 private Client client = null;
42 public static final String HTTP_STATUS_DESCR = "response_description";
43 public static final String HTTP_STATUS = "response_status";
44 private APPLICATION_TYPE appType = null;
45 private String userName;
46 private String password;
49 * CRUDImpl - constructor with arguments.
50 * @param appType - content type of the http response.
51 * @param userName - username of the controller to connect to the server.
52 * @param password - password of the controller to connect to the server.
54 public CRUDImpl(APPLICATION_TYPE appType, String userName, String password) {
55 this.appType = appType;
56 this.client = Client.create();
57 this.userName = userName;
58 this.password = password;
59 this.client.addFilter(new HTTPBasicAuthFilter(userName, password));
63 * Get the Jersey client
67 private Client getClient() {
72 * Create the web resource with the URL
78 * Web resource instance
80 protected Builder getWebResource(String url, Map<String, String> queryParams, Map<String, Object> headers) {
81 WebResource webResource = getClient().resource(url);
84 * Set query parameters
86 if (queryParams != null) {
87 for (Entry<String, String> entry: queryParams.entrySet()) {
88 webResource.queryParam(entry.getKey(), entry.getValue());
95 Builder builder = null;
96 if (headers != null) {
97 for (Entry<String, Object> entry: headers.entrySet()) {
98 if (builder == null) {
99 builder = webResource.header(entry.getKey(), entry.getValue());
101 builder = builder.header(entry.getKey(), entry.getValue());
105 return (builder != null ? builder : webResource.getRequestBuilder());
109 * To create a JSON object for a particular HTTP response
114 * @throws JSONException
116 private JSONObject getHttpResponseObject(HTTP_RESPONSE response) throws JSONException {
117 JSONObject object = new JSONObject();
118 object.put(HTTP_STATUS_DESCR, response.getDescription());
119 object.put(HTTP_STATUS, response.getStatus());
129 * @throws VTNClientException if request does not return 'success'
130 * @throws JSONException
131 * @throws ConnectException
133 public String doGET(String url, Map<String, Object> headers) throws VTNClientException, JSONException, ConnectException {
135 ClientResponse response = getWebResource(url, null, headers).accept(appType.getType()).get(ClientResponse.class);
136 if (response.getStatus() == HTTP_RESPONSE.NO_CONTENT.getStatus()) {
137 return getHttpResponseObject(HTTP_RESPONSE.NO_CONTENT).toString();
139 if (response.getStatus() == HTTP_RESPONSE.UNAUTHORIZED.getStatus()) {
140 LOG.error("Unauthorized access :HTTP error code : {}", response.getStatus());
141 LOG.error(getHttpResponseObject(HTTP_RESPONSE.UNAUTHORIZED).toString());
142 throw new VTNClientException("\n\nFailed to connect to ODL Controller due to unauthorized access..."
143 + "\nPlease check Username/Password...");
144 } else if (response.getStatus() != HTTP_RESPONSE.OPERATION_SUCCESS.getStatus()) {
145 LOG.error("GET RequestFailed :HTTP error code : {}", response.getStatus());
146 throw new VTNClientException("\n\nPage not found - " + response.getStatus());
148 return response.getEntity(String.class);
150 } catch (ClientHandlerException e) {
151 LOG.error("An exception occured - ", e);
152 throw new VTNClientException("\n\nFailed to connect to ODL Controller..."
153 + "\nPlease check Controller is running or IP address/Port number is incorrect...");
158 * Send a POST request
162 * Object to send along POST request
167 * @throws VTNClientException if request does not return 'success'
168 * @throws JSONException
170 public String doPOST(String url, Object obj, Map<String, Object> headers) throws VTNClientException, ConnectException, JSONException {
179 * Object to send along PUT request
184 * @throws VTNClientException if request does not return 'success'
185 * @throws JSONException
187 public String doPUT(String url, Object obj, Map<String, Object> headers) throws VTNClientException, ConnectException, JSONException {
192 * Send a DELETE request
196 * Object to send along DELETE request
201 * @throws VTNClientException if request does not return 'success'
202 * @throws JSONException
204 public String doDELETE(String url, Object obj, Map<String, Object> headers) throws VTNClientException, ConnectException, JSONException {