2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.adapter.chef.chefapi;
25 import java.io.IOException;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28 import java.util.TimeZone;
30 import org.apache.http.*;
31 import org.apache.http.client.*;
32 import org.apache.http.client.methods.*;
33 import org.apache.http.impl.client.*;
34 import org.apache.http.util.EntityUtils;
35 import org.openecomp.appc.adapter.chef.chefclient.Utils;
37 import javax.net.ssl.SSLContext;
39 import org.apache.http.HttpEntity;
40 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
41 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
42 import org.apache.http.impl.client.HttpClients;
43 import org.apache.http.ssl.SSLContexts;
45 public class ApiMethod {
46 private HttpClient client = null;
47 protected HttpRequestBase method = null;
48 protected HttpResponse response = null;
49 protected String reqBody = "";
50 protected String userId = "";
51 protected String pemPath = "";
52 protected String chefPath = "";
53 protected String organizations = "";
54 protected int resCode=0;
55 protected String responseBody="";
56 private String methodName = "GET";
57 public String test = "";
58 private int returnCode;
60 public ApiMethod(String methodName) {
61 client=HttpClients.createDefault();
62 this.methodName = methodName;
65 public ApiMethod execute() {
66 String hashedPath = Utils.sha1AndBase64("/organizations/"+organizations+chefPath);
67 String hashedBody = Utils.sha1AndBase64(reqBody);
69 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
70 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
71 String timeStamp = sdf.format(new Date());
72 timeStamp = timeStamp.replace(" ", "T");
73 timeStamp = timeStamp + "Z";
75 StringBuilder sb = new StringBuilder();
76 sb.append("Method:").append(methodName).append("\n");
77 sb.append("Hashed Path:").append(hashedPath).append("\n");
78 sb.append("X-Ops-Content-Hash:").append(hashedBody).append("\n");
79 sb.append("X-Ops-Timestamp:").append(timeStamp).append("\n");
80 sb.append("X-Ops-UserId:").append(userId);
81 test = test + "sb " + sb + "\n";
83 String auth_String = Utils.signWithRSA(sb.toString(), pemPath);
84 String[] auth_headers = Utils.splitAs60(auth_String);
86 method.addHeader("Content-type", "application/json");
87 method.addHeader("X-Ops-Timestamp", timeStamp);
88 method.addHeader("X-Ops-Userid", userId);
89 method.addHeader("X-Chef-Version", "12.4.1");
90 method.addHeader("Accept", "application/json");
91 method.addHeader("X-Ops-Content-Hash", hashedBody);
92 method.addHeader("X-Ops-Sign", "version=1.0");
94 for (int i = 0; i < auth_headers.length; i++) {
95 method.addHeader("X-Ops-Authorization-" + (i + 1), auth_headers[i]);
98 response = client.execute(method);
99 resCode = response.getStatusLine().getStatusCode();
100 HttpEntity entity1 = response.getEntity();
101 responseBody = EntityUtils.toString(entity1);}
104 responseBody=ex.getMessage();
109 public void setHeaders(Header[] headers) {
110 for (Header header : headers) {
111 this.method.addHeader(header);
115 public String getResponseBodyAsString() {
119 public int getReturnCode() {
123 public String getReqBody() {
127 public void setReqBody(String body) {
131 public String getUserId() {
135 public void setUserId(String userId) {
136 this.userId = userId;
139 public String getPemPath() {
143 public void setPemPath(String pemPath) {
144 this.pemPath = pemPath;
147 public String getChefPath() {
151 public void setChefPath(String chefPath) {
152 this.chefPath = chefPath;
155 public String getOrganizations() {
156 return organizations;
159 public void setOrganizations(String organizations) {
160 this.organizations = organizations;