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;
56 public ApiMethod(String methodName) {
57 client=HttpClients.createDefault();
58 this.methodName = methodName;
61 public ApiMethod createRequest(){
62 String hashedPath = Utils.sha1AndBase64("/organizations/"+organizations+chefPath);
63 String hashedBody = Utils.sha1AndBase64(reqBody);
65 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
66 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
67 String timeStamp = sdf.format(new Date());
68 timeStamp = timeStamp.replace(" ", "T");
69 timeStamp = timeStamp + "Z";
71 StringBuilder sb = new StringBuilder();
72 sb.append("Method:").append(methodName).append("\n");
73 sb.append("Hashed Path:").append(hashedPath).append("\n");
74 sb.append("X-Ops-Content-Hash:").append(hashedBody).append("\n");
75 sb.append("X-Ops-Timestamp:").append(timeStamp).append("\n");
76 sb.append("X-Ops-UserId:").append(userId);
77 test = test + "sb " + sb + "\n";
79 String auth_String = Utils.signWithRSA(sb.toString(), pemPath);
80 String[] auth_headers = Utils.splitAs60(auth_String);
82 method.addHeader("Content-type", "application/json");
83 method.addHeader("X-Ops-Timestamp", timeStamp);
84 method.addHeader("X-Ops-Userid", userId);
85 method.addHeader("X-Chef-Version", "12.4.1");
86 method.addHeader("Accept", "application/json");
87 method.addHeader("X-Ops-Content-Hash", hashedBody);
88 method.addHeader("X-Ops-Sign", "version=1.0");
90 for (int i = 0; i < auth_headers.length; i++) {
91 method.addHeader("X-Ops-Authorization-" + (i + 1), auth_headers[i]);
94 * test=test+this.method.getMethod()+"\n"; Header[]
95 * RHS=this.method.getHeaders(); for (int i = 0; i < RHS.length; i++) {
96 * test=test+RHS[i]+"\n"; } test=test+this.reqBody+"\n";
101 public ApiMethod execute() {
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;