2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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=========================================================
22 package org.openecomp.appc.adapter.chef.chefapi;
24 import java.io.IOException;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27 import java.util.TimeZone;
29 import org.apache.http.*;
30 import org.apache.http.client.*;
31 import org.apache.http.client.methods.*;
32 import org.apache.http.impl.client.*;
33 import org.apache.http.util.EntityUtils;
34 import org.openecomp.appc.adapter.chef.chefclient.Utils;
36 import javax.net.ssl.SSLContext;
38 import org.apache.http.HttpEntity;
39 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
40 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
41 import org.apache.http.impl.client.HttpClients;
42 import org.apache.http.ssl.SSLContexts;
44 public class ApiMethod {
45 private HttpClient client = null;
46 protected HttpRequestBase method = null;
47 protected HttpResponse response = null;
48 protected String reqBody = "";
49 protected String userId = "";
50 protected String pemPath = "";
51 protected String chefPath = "";
52 protected String organizations = "";
53 protected int resCode=0;
54 protected String responseBody="";
55 private String methodName = "GET";
56 public String test = "";
57 private int returnCode;
59 public ApiMethod(String methodName) {
60 client=HttpClients.createDefault();
61 this.methodName = methodName;
64 public ApiMethod execute() {
65 String hashedPath = Utils.sha1AndBase64("/organizations/"+organizations+chefPath);
66 String hashedBody = Utils.sha1AndBase64(reqBody);
68 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
69 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
70 String timeStamp = sdf.format(new Date());
71 timeStamp = timeStamp.replace(" ", "T");
72 timeStamp = timeStamp + "Z";
74 StringBuilder sb = new StringBuilder();
75 sb.append("Method:").append(methodName).append("\n");
76 sb.append("Hashed Path:").append(hashedPath).append("\n");
77 sb.append("X-Ops-Content-Hash:").append(hashedBody).append("\n");
78 sb.append("X-Ops-Timestamp:").append(timeStamp).append("\n");
79 sb.append("X-Ops-UserId:").append(userId);
80 test = test + "sb " + sb + "\n";
82 String auth_String = Utils.signWithRSA(sb.toString(), pemPath);
83 String[] auth_headers = Utils.splitAs60(auth_String);
85 method.addHeader("Content-type", "application/json");
86 method.addHeader("X-Ops-Timestamp", timeStamp);
87 method.addHeader("X-Ops-Userid", userId);
88 method.addHeader("X-Chef-Version", "12.4.1");
89 method.addHeader("Accept", "application/json");
90 method.addHeader("X-Ops-Content-Hash", hashedBody);
91 method.addHeader("X-Ops-Sign", "version=1.0");
93 for (int i = 0; i < auth_headers.length; i++) {
94 method.addHeader("X-Ops-Authorization-" + (i + 1), auth_headers[i]);
97 response = client.execute(method);
98 resCode = response.getStatusLine().getStatusCode();
99 HttpEntity entity1 = response.getEntity();
100 responseBody = EntityUtils.toString(entity1);}
103 responseBody=ex.getMessage();
108 public void setHeaders(Header[] headers) {
109 for (Header header : headers) {
110 this.method.addHeader(header);
114 public String getResponseBodyAsString() {
118 public int getReturnCode() {
122 public String getReqBody() {
126 public void setReqBody(String body) {
130 public String getUserId() {
134 public void setUserId(String userId) {
135 this.userId = userId;
138 public String getPemPath() {
142 public void setPemPath(String pemPath) {
143 this.pemPath = pemPath;
146 public String getChefPath() {
150 public void setChefPath(String chefPath) {
151 this.chefPath = chefPath;
154 public String getOrganizations() {
155 return organizations;
158 public void setOrganizations(String organizations) {
159 this.organizations = organizations;