ChefAdapter code clean-up
[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  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.adapter.chef.chefapi;
26
27 import java.io.IOException;
28 import java.text.SimpleDateFormat;
29 import java.util.Date;
30 import java.util.TimeZone;
31
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;
38
39 import javax.net.ssl.SSLContext;
40 import java.io.File;
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;
46
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;
61
62     public ApiMethod(String methodName) {
63         client=HttpClients.createDefault();
64         this.methodName = methodName;
65     }
66
67     public ApiMethod execute() {
68         String hashedPath = Utils.sha1AndBase64("/organizations/"+organizations+chefPath);
69         String hashedBody = Utils.sha1AndBase64(reqBody);
70
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";
76
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";
84
85         String auth_String = Utils.signWithRSA(sb.toString(), pemPath);
86         String[] auth_headers = Utils.splitAs60(auth_String);
87
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");
95
96         for (int i = 0; i < auth_headers.length; i++) {
97             method.addHeader("X-Ops-Authorization-" + (i + 1), auth_headers[i]);
98         }
99         /*
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";
103          */
104         try{
105         response = client.execute(method);
106         resCode = response.getStatusLine().getStatusCode();
107         HttpEntity entity1 = response.getEntity();
108         responseBody = EntityUtils.toString(entity1);}
109         catch(Exception ex){
110             resCode=500;
111             responseBody=ex.getMessage();
112         }
113         return this;
114     }
115
116     public void setHeaders(Header[] headers) {
117         for (Header header : headers) {
118             this.method.addHeader(header);
119         }
120     }
121
122     public String getResponseBodyAsString() {
123         return responseBody;
124     }
125
126     public int getReturnCode() {
127         return resCode;
128     }
129
130     public String getReqBody() {
131         return reqBody;
132     }
133
134     public void setReqBody(String body) {
135         this.reqBody = body;
136     }
137
138     public String getUserId() {
139         return userId;
140     }
141
142     public void setUserId(String userId) {
143         this.userId = userId;
144     }
145
146     public String getPemPath() {
147         return pemPath;
148     }
149
150     public void setPemPath(String pemPath) {
151         this.pemPath = pemPath;
152     }
153
154     public String getChefPath() {
155         return chefPath;
156     }
157
158     public void setChefPath(String chefPath) {
159         this.chefPath = chefPath;
160     }
161
162     public String getOrganizations() {
163         return organizations;
164     }
165
166     public void setOrganizations(String organizations) {
167         this.organizations = organizations;
168     }
169 }