Change nexus values to properties
[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 //      final String KEY_STORE_PATH = "/tmp/chef/trusted_certs/mykeystore.jks";
59 //      final String KEY_STORE_PASSWORD = "changeit";
60
61         public ApiMethod(String methodName) {
62 /*              try {
63                         SSLContext sslcontext = SSLContexts.custom()
64                         .loadTrustMaterial(new File(KEY_STORE_PATH), KEY_STORE_PASSWORD.toCharArray(),
65                                 new TrustSelfSignedStrategy())
66                         .build();
67                         SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
68                         sslcontext,
69                         new String[] { "TLSv1" },
70                         null,
71                         SSLConnectionSocketFactory.getDefaultHostnameVerifier());
72                  client = HttpClients.custom()
73                         .setSSLSocketFactory(sslsf)
74                         .build();
75                 } catch (Exception e) {
76                         // TODO Auto-generated catch block
77                         e.printStackTrace();
78                 }*/
79                 client=HttpClients.createDefault();
80                 this.methodName = methodName;
81         }
82
83         public ApiMethod execute() {
84                 String hashedPath = Utils.sha1AndBase64("/organizations/"+organizations+chefPath);
85                 String hashedBody = Utils.sha1AndBase64(reqBody);
86
87                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
88                 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
89                 String timeStamp = sdf.format(new Date());
90                 timeStamp = timeStamp.replace(" ", "T");
91                 timeStamp = timeStamp + "Z";
92
93                 StringBuilder sb = new StringBuilder();
94                 sb.append("Method:").append(methodName).append("\n");
95                 sb.append("Hashed Path:").append(hashedPath).append("\n");
96                 sb.append("X-Ops-Content-Hash:").append(hashedBody).append("\n");
97                 sb.append("X-Ops-Timestamp:").append(timeStamp).append("\n");
98                 sb.append("X-Ops-UserId:").append(userId);
99                 test = test + "sb " + sb + "\n";
100
101                 String auth_String = Utils.signWithRSA(sb.toString(), pemPath);
102                 String[] auth_headers = Utils.splitAs60(auth_String);
103
104                 method.addHeader("Content-type", "application/json");
105                 method.addHeader("X-Ops-Timestamp", timeStamp);
106                 method.addHeader("X-Ops-Userid", userId);
107                 method.addHeader("X-Chef-Version", "12.4.1");
108                 method.addHeader("Accept", "application/json");
109                 method.addHeader("X-Ops-Content-Hash", hashedBody);
110                 method.addHeader("X-Ops-Sign", "version=1.0");
111
112                 for (int i = 0; i < auth_headers.length; i++) {
113                         method.addHeader("X-Ops-Authorization-" + (i + 1), auth_headers[i]);
114                 }
115                 /*
116                  * test=test+this.method.getMethod()+"\n"; Header[]
117                  * RHS=this.method.getHeaders(); for (int i = 0; i < RHS.length; i++) {
118                  * test=test+RHS[i]+"\n"; } test=test+this.reqBody+"\n";
119                  */
120                 try{
121                 response = client.execute(method);
122                 resCode = response.getStatusLine().getStatusCode();
123                 HttpEntity entity1 = response.getEntity();
124                 responseBody = EntityUtils.toString(entity1);}
125                 catch(Exception ex){
126                         resCode=500;
127                         responseBody=ex.getMessage();
128                 }
129                 return this;
130         }
131
132         public void setHeaders(Header[] headers) {
133                 for (Header header : headers) {
134                         this.method.addHeader(header);
135                 }
136         }
137
138         public String getResponseBodyAsString() {
139                 return responseBody;
140         }
141
142         public int getReturnCode() {
143                 return resCode;
144         }
145
146         public String getReqBody() {
147                 return reqBody;
148         }
149
150         public void setReqBody(String body) {
151                 this.reqBody = body;
152         }
153
154         public String getUserId() {
155                 return userId;
156         }
157
158         public void setUserId(String userId) {
159                 this.userId = userId;
160         }
161
162         public String getPemPath() {
163                 return pemPath;
164         }
165
166         public void setPemPath(String pemPath) {
167                 this.pemPath = pemPath;
168         }
169         
170         public String getChefPath() {
171                 return chefPath;
172         }
173
174         public void setChefPath(String chefPath) {
175                 this.chefPath = chefPath;
176         }
177         
178         public String getOrganizations() {
179                 return organizations;
180         }
181
182         public void setOrganizations(String organizations) {
183                 this.organizations = organizations;
184         }
185 }