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.openecomp.appc.adapter.chef.chefapi;
27 import java.io.IOException;
28 import java.text.SimpleDateFormat;
29 import java.util.Date;
30 import java.util.TimeZone;
32 import org.apache.http.*;
33 import org.apache.http.client.*;
34 import org.apache.http.client.methods.*;
35 import org.apache.http.impl.client.*;
36 import org.apache.http.util.EntityUtils;
37 import org.openecomp.appc.adapter.chef.chefclient.Utils;
39 import javax.net.ssl.SSLContext;
41 import org.apache.http.HttpEntity;
42 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
43 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
44 import org.apache.http.impl.client.HttpClients;
45 import org.apache.http.ssl.SSLContexts;
47 public class ApiMethod {
48 private HttpClient client = null;
49 protected HttpRequestBase method = null;
50 protected HttpResponse response = null;
51 protected String reqBody = "";
52 protected String userId = "";
53 protected String pemPath = "";
54 protected String chefPath = "";
55 protected String organizations = "";
56 protected int resCode=0;
57 protected String responseBody="";
58 private String methodName = "GET";
59 public String test = "";
60 private int returnCode;
62 public ApiMethod(String methodName) {
63 client=HttpClients.createDefault();
64 this.methodName = methodName;
67 public ApiMethod execute() {
68 String hashedPath = Utils.sha1AndBase64("/organizations/"+organizations+chefPath);
69 String hashedBody = Utils.sha1AndBase64(reqBody);
71 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
72 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
73 String timeStamp = sdf.format(new Date());
74 timeStamp = timeStamp.replace(" ", "T");
75 timeStamp = timeStamp + "Z";
77 StringBuilder sb = new StringBuilder();
78 sb.append("Method:").append(methodName).append("\n");
79 sb.append("Hashed Path:").append(hashedPath).append("\n");
80 sb.append("X-Ops-Content-Hash:").append(hashedBody).append("\n");
81 sb.append("X-Ops-Timestamp:").append(timeStamp).append("\n");
82 sb.append("X-Ops-UserId:").append(userId);
83 test = test + "sb " + sb + "\n";
85 String auth_String = Utils.signWithRSA(sb.toString(), pemPath);
86 String[] auth_headers = Utils.splitAs60(auth_String);
88 method.addHeader("Content-type", "application/json");
89 method.addHeader("X-Ops-Timestamp", timeStamp);
90 method.addHeader("X-Ops-Userid", userId);
91 method.addHeader("X-Chef-Version", "12.4.1");
92 method.addHeader("Accept", "application/json");
93 method.addHeader("X-Ops-Content-Hash", hashedBody);
94 method.addHeader("X-Ops-Sign", "version=1.0");
96 for (int i = 0; i < auth_headers.length; i++) {
97 method.addHeader("X-Ops-Authorization-" + (i + 1), auth_headers[i]);
100 * test=test+this.method.getMethod()+"\n"; Header[]
101 * RHS=this.method.getHeaders(); for (int i = 0; i < RHS.length; i++) {
102 * test=test+RHS[i]+"\n"; } test=test+this.reqBody+"\n";
105 response = client.execute(method);
106 resCode = response.getStatusLine().getStatusCode();
107 HttpEntity entity1 = response.getEntity();
108 responseBody = EntityUtils.toString(entity1);}
111 responseBody=ex.getMessage();
116 public void setHeaders(Header[] headers) {
117 for (Header header : headers) {
118 this.method.addHeader(header);
122 public String getResponseBodyAsString() {
126 public int getReturnCode() {
130 public String getReqBody() {
134 public void setReqBody(String body) {
138 public String getUserId() {
142 public void setUserId(String userId) {
143 this.userId = userId;
146 public String getPemPath() {
150 public void setPemPath(String pemPath) {
151 this.pemPath = pemPath;
154 public String getChefPath() {
158 public void setChefPath(String chefPath) {
159 this.chefPath = chefPath;
162 public String getOrganizations() {
163 return organizations;
166 public void setOrganizations(String organizations) {
167 this.organizations = organizations;