c3ba7c1f3db73497dcaad23d1b7941ae4bbeda92
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / main / java / org / openecomp / appc / adapter / chef / chefapi / ApiMethod.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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  */
21
22 package org.openecomp.appc.adapter.chef.chefapi;
23
24 import java.io.IOException;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27 import java.util.TimeZone;
28
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;
35
36 import javax.net.ssl.SSLContext;
37 import java.io.File;
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;
43
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;
58
59         public ApiMethod(String methodName) {
60                 client=HttpClients.createDefault();
61                 this.methodName = methodName;
62         }
63
64         public ApiMethod execute() {
65                 String hashedPath = Utils.sha1AndBase64("/organizations/"+organizations+chefPath);
66                 String hashedBody = Utils.sha1AndBase64(reqBody);
67
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";
73
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";
81
82                 String auth_String = Utils.signWithRSA(sb.toString(), pemPath);
83                 String[] auth_headers = Utils.splitAs60(auth_String);
84
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");
92
93                 for (int i = 0; i < auth_headers.length; i++) {
94                         method.addHeader("X-Ops-Authorization-" + (i + 1), auth_headers[i]);
95                 }
96                 try{
97                 response = client.execute(method);
98                 resCode = response.getStatusLine().getStatusCode();
99                 HttpEntity entity1 = response.getEntity();
100                 responseBody = EntityUtils.toString(entity1);}
101                 catch(Exception ex){
102                         resCode=500;
103                         responseBody=ex.getMessage();
104                 }
105                 return this;
106         }
107
108         public void setHeaders(Header[] headers) {
109                 for (Header header : headers) {
110                         this.method.addHeader(header);
111                 }
112         }
113
114         public String getResponseBodyAsString() {
115                 return responseBody;
116         }
117
118         public int getReturnCode() {
119                 return resCode;
120         }
121
122         public String getReqBody() {
123                 return reqBody;
124         }
125
126         public void setReqBody(String body) {
127                 this.reqBody = body;
128         }
129
130         public String getUserId() {
131                 return userId;
132         }
133
134         public void setUserId(String userId) {
135                 this.userId = userId;
136         }
137
138         public String getPemPath() {
139                 return pemPath;
140         }
141
142         public void setPemPath(String pemPath) {
143                 this.pemPath = pemPath;
144         }
145         
146         public String getChefPath() {
147                 return chefPath;
148         }
149
150         public void setChefPath(String chefPath) {
151                 this.chefPath = chefPath;
152         }
153         
154         public String getOrganizations() {
155                 return organizations;
156         }
157
158         public void setOrganizations(String organizations) {
159                 this.organizations = organizations;
160         }
161 }