30faba245a2fad1954d4e9d5d590b722cf44b4ef
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.adapter.chef.chefapi;
24
25 import java.io.IOException;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28 import java.util.TimeZone;
29
30 import org.apache.http.*;
31 import org.apache.http.client.*;
32 import org.apache.http.client.methods.*;
33 import org.apache.http.impl.client.*;
34 import org.apache.http.util.EntityUtils;
35 import org.openecomp.appc.adapter.chef.chefclient.Utils;
36
37 import javax.net.ssl.SSLContext;
38 import java.io.File;
39 import org.apache.http.HttpEntity;
40 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
41 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
42 import org.apache.http.impl.client.HttpClients;
43 import org.apache.http.ssl.SSLContexts;
44
45 public class ApiMethod {
46         private HttpClient client = null;
47         protected HttpRequestBase method = null;
48         protected HttpResponse response = null;
49         protected String reqBody = "";
50         protected String userId = "";
51         protected String pemPath = "";
52         protected String chefPath = "";
53         protected String organizations = "";
54         protected int resCode=0;
55         protected String responseBody="";
56         private String methodName = "GET";
57         public String test = "";
58         private int returnCode;
59
60         public ApiMethod(String methodName) {
61                 client=HttpClients.createDefault();
62                 this.methodName = methodName;
63         }
64
65         public ApiMethod execute() {
66                 String hashedPath = Utils.sha1AndBase64("/organizations/"+organizations+chefPath);
67                 String hashedBody = Utils.sha1AndBase64(reqBody);
68
69                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
70                 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
71                 String timeStamp = sdf.format(new Date());
72                 timeStamp = timeStamp.replace(" ", "T");
73                 timeStamp = timeStamp + "Z";
74
75                 StringBuilder sb = new StringBuilder();
76                 sb.append("Method:").append(methodName).append("\n");
77                 sb.append("Hashed Path:").append(hashedPath).append("\n");
78                 sb.append("X-Ops-Content-Hash:").append(hashedBody).append("\n");
79                 sb.append("X-Ops-Timestamp:").append(timeStamp).append("\n");
80                 sb.append("X-Ops-UserId:").append(userId);
81                 test = test + "sb " + sb + "\n";
82
83                 String auth_String = Utils.signWithRSA(sb.toString(), pemPath);
84                 String[] auth_headers = Utils.splitAs60(auth_String);
85
86                 method.addHeader("Content-type", "application/json");
87                 method.addHeader("X-Ops-Timestamp", timeStamp);
88                 method.addHeader("X-Ops-Userid", userId);
89                 method.addHeader("X-Chef-Version", "12.4.1");
90                 method.addHeader("Accept", "application/json");
91                 method.addHeader("X-Ops-Content-Hash", hashedBody);
92                 method.addHeader("X-Ops-Sign", "version=1.0");
93
94                 for (int i = 0; i < auth_headers.length; i++) {
95                         method.addHeader("X-Ops-Authorization-" + (i + 1), auth_headers[i]);
96                 }
97                 try{
98                 response = client.execute(method);
99                 resCode = response.getStatusLine().getStatusCode();
100                 HttpEntity entity1 = response.getEntity();
101                 responseBody = EntityUtils.toString(entity1);}
102                 catch(Exception ex){
103                         resCode=500;
104                         responseBody=ex.getMessage();
105                 }
106                 return this;
107         }
108
109         public void setHeaders(Header[] headers) {
110                 for (Header header : headers) {
111                         this.method.addHeader(header);
112                 }
113         }
114
115         public String getResponseBodyAsString() {
116                 return responseBody;
117         }
118
119         public int getReturnCode() {
120                 return resCode;
121         }
122
123         public String getReqBody() {
124                 return reqBody;
125         }
126
127         public void setReqBody(String body) {
128                 this.reqBody = body;
129         }
130
131         public String getUserId() {
132                 return userId;
133         }
134
135         public void setUserId(String userId) {
136                 this.userId = userId;
137         }
138
139         public String getPemPath() {
140                 return pemPath;
141         }
142
143         public void setPemPath(String pemPath) {
144                 this.pemPath = pemPath;
145         }
146         
147         public String getChefPath() {
148                 return chefPath;
149         }
150
151         public void setChefPath(String chefPath) {
152                 this.chefPath = chefPath;
153         }
154         
155         public String getOrganizations() {
156                 return organizations;
157         }
158
159         public void setOrganizations(String organizations) {
160                 this.organizations = organizations;
161         }
162 }