2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the “License”);
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.openecomp.portalapp.portal.scheduler;
40 import java.util.Collections;
42 import javax.ws.rs.client.Client;
43 import javax.ws.rs.client.Entity;
44 import javax.ws.rs.core.MediaType;
45 import javax.ws.rs.core.MultivaluedHashMap;
46 import javax.ws.rs.core.Response;
48 import org.apache.commons.codec.binary.Base64;
49 import org.json.simple.JSONObject;
50 import org.openecomp.portalapp.portal.scheduler.client.HttpBasicClient;
51 import org.openecomp.portalapp.portal.scheduler.client.HttpsBasicClient;
52 import org.openecomp.portalsdk.core.util.SystemProperties;
53 import org.springframework.stereotype.Service;
54 import org.openecomp.portalapp.portal.scheduler.restobjects.RestObject;
58 public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {
60 private static Client client = null;
62 private MultivaluedHashMap<String, Object> commonHeaders;
64 public SchedulerRestInterface() {
68 public void initRestClient()
70 final String methodname = "initRestClient()";
72 final String username = "";//SystemProperties.getProperty(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
73 //final String password = "";//SystemProperties.getProperty(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
74 final String scheduler_url = "";//SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL);
75 final String decrypted_password = "";//Password.deobfuscate(password);
77 String authString = username + ":" + decrypted_password;
79 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
80 String authStringEnc = new String(authEncBytes);
82 commonHeaders = new MultivaluedHashMap<String, Object> ();
83 commonHeaders.put("Authorization", Collections.singletonList((Object) ("Basic " + authStringEnc)));
85 boolean use_ssl = true;
86 if ( (scheduler_url != null) && ( !(scheduler_url.isEmpty()) ) ) {
87 if ( scheduler_url.startsWith("https")) {
99 client = HttpsBasicClient.getClient();
103 client = HttpBasicClient.getClient();
105 } catch (Exception e) {
106 System.out.println( methodname + " Unable to get the SSL client");
111 @SuppressWarnings("unchecked")
112 public <T> void Get (T t, String sourceId, String path, org.openecomp.portalapp.portal.scheduler.restobjects.RestObject<T> restObject ) throws Exception {
114 String methodName = "Get";
115 String url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
118 System.out.println( "<== URL FOR GET : " + url + "\n");
122 final Response cres = client.target(url)
124 .accept("application/json")
125 .headers(commonHeaders)
128 int status = cres.getStatus();
129 restObject.setStatusCode (status);
132 t = (T) cres.readEntity(t.getClass());
136 throw new Exception(methodName + " with status="+ status + ", url= " + url );
142 @SuppressWarnings("unchecked")
143 public <T> void Post(T t, JSONObject requestDetails, String path, RestObject<T> restObject) throws Exception {
145 String methodName = "Post";
146 String url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
148 System.out.println( "<== URL FOR POST : " + url + "\n");
154 // Change the content length
155 final Response cres = client.target(url)
157 .accept("application/json")
158 .headers(commonHeaders)
159 //.header("content-length", 201)
160 //.header("X-FromAppId", sourceID)
161 .post(Entity.entity(requestDetails, MediaType.APPLICATION_JSON));
164 t = (T) cres.readEntity(t.getClass());
167 catch ( Exception e ) {
169 System.out.println("<== " + methodName + " : No response entity, this is probably ok, e=" + e.getMessage());
172 int status = cres.getStatus();
173 restObject.setStatusCode (status);
175 if ( status >= 200 && status <= 299 ) {
177 System.out.println( "<== " + methodName + " : REST api POST was successful!" + "\n");
180 System.out.println( "<== " + methodName + " : FAILED with http status : "+status+", url = " + url + "\n");
183 } catch (Exception e)
185 System.out.println( "<== " + methodName + " : with url="+url+ ", Exception: " + e.toString() + "\n");
190 @SuppressWarnings("unchecked")
191 public <T> void Delete(T t, JSONObject requestDetails, String sourceID, String path, RestObject<T> restObject) {
194 Response cres = null;
199 url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
201 cres = client.target(url)
203 .accept("application/json")
204 .headers(commonHeaders)
206 .build("DELETE", Entity.entity(requestDetails, MediaType.APPLICATION_JSON)).invoke();
207 // .method("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON));
208 //.delete(Entity.entity(r, MediaType.APPLICATION_JSON));
210 int status = cres.getStatus();
211 restObject.setStatusCode (status);
214 t = (T) cres.readEntity(t.getClass());
217 catch ( Exception e ) {
227 public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException
229 return clazz.newInstance();
233 public void logRequest(JSONObject requestDetails) {
234 // TODO Auto-generated method stub