2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.chef.chefapi;
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
29 import java.util.TimeZone;
31 import org.apache.http.HttpResponse;
32 import org.apache.http.Header;
33 import org.apache.http.client.HttpClient;
34 import org.apache.http.client.methods.HttpRequestBase;
35 import org.apache.http.util.EntityUtils;
36 import org.onap.appc.adapter.chef.chefclient.Utils;
38 import org.apache.http.HttpEntity;
39 import org.apache.http.impl.client.HttpClients;
41 public class ApiMethod {
42 private HttpClient client = null;
43 protected HttpRequestBase method = null;
44 protected HttpResponse response = null;
45 protected String reqBody = "";
46 protected String userId = "";
47 protected String pemPath = "";
48 protected String chefPath = "";
49 protected String organizations = "";
50 protected int resCode=0;
51 protected String responseBody="";
52 private String methodName = "GET";
53 public String test = "";
54 private int returnCode;
55 final String KEY_STORE_PATH = "/etc/chef/trusted_certs/mykeystore.jks";
56 final String KEY_STORE_PASSWORD = "adminadmin";
59 System.setProperty("javax.net.ssl.trustStore", "/opt/onap/appc/chef/chefServerSSL.jks");
60 System.setProperty("javax.net.ssl.trustStorePassword", "adminadmin");
63 public ApiMethod(String methodName) {
64 client=HttpClients.createDefault();
65 this.methodName = methodName;
70 public ApiMethod execute() {
71 String hashedPath = Utils.sha1AndBase64("/organizations/"+organizations+chefPath);
72 String hashedBody = Utils.sha1AndBase64(reqBody);
74 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
75 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
76 String timeStamp = sdf.format(new Date());
77 timeStamp = timeStamp.replace(" ", "T");
78 timeStamp = timeStamp + "Z";
80 StringBuilder sb = new StringBuilder();
81 sb.append("Method:").append(methodName).append("\n");
82 sb.append("Hashed Path:").append(hashedPath).append("\n");
83 sb.append("X-Ops-Content-Hash:").append(hashedBody).append("\n");
84 sb.append("X-Ops-Timestamp:").append(timeStamp).append("\n");
85 sb.append("X-Ops-UserId:").append(userId);
86 test = test + "sb " + sb + "\n";
88 String auth_String = Utils.signWithRSA(sb.toString(), pemPath);
89 String[] auth_headers = Utils.splitAs60(auth_String);
91 method.addHeader("Content-type", "application/json");
92 method.addHeader("X-Ops-Timestamp", timeStamp);
93 method.addHeader("X-Ops-Userid", userId);
94 method.addHeader("X-Chef-Version", "12.4.1");
95 method.addHeader("Accept", "application/json");
96 method.addHeader("X-Ops-Content-Hash", hashedBody);
97 method.addHeader("X-Ops-Sign", "version=1.0");
99 for (int i = 0; i < auth_headers.length; i++) {
100 method.addHeader("X-Ops-Authorization-" + (i + 1), auth_headers[i]);
103 response = client.execute(method);
104 resCode = response.getStatusLine().getStatusCode();
105 HttpEntity entity1 = response.getEntity();
106 responseBody = EntityUtils.toString(entity1);}
109 responseBody=ex.getMessage();
114 public void setHeaders(Header[] headers) {
115 for (Header header : headers) {
116 this.method.addHeader(header);
120 public String getResponseBodyAsString() {
124 public int getReturnCode() {
128 public String getReqBody() {
132 public void setReqBody(String body) {
136 public String getUserId() {
140 public void setUserId(String userId) {
141 this.userId = userId;
144 public String getPemPath() {
148 public void setPemPath(String pemPath) {
149 this.pemPath = pemPath;
152 public String getChefPath() {
156 public void setChefPath(String chefPath) {
157 this.chefPath = chefPath;
160 public String getOrganizations() {
161 return organizations;
164 public void setOrganizations(String organizations) {
165 this.organizations = organizations;