Applying license changes to all files
[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         final String KEY_STORE_PATH = "/etc/chef/trusted_certs/mykeystore.jks";
62         final String KEY_STORE_PASSWORD = "adminadmin";
63         static
64 {
65         System.setProperty("javax.net.ssl.trustStore", "/opt/app/bvc/chef/chefServerSSL.jks");
66         System.setProperty("javax.net.ssl.trustStorePassword", "adminadmin");
67                 }
68         public ApiMethod(String methodName) {
69
70                 client=HttpClients.createDefault();
71                 this.methodName = methodName;
72         }
73
74         public ApiMethod execute() {
75                 String hashedPath = Utils.sha1AndBase64("/organizations/"+organizations+chefPath);
76                 String hashedBody = Utils.sha1AndBase64(reqBody);
77
78                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
79                 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
80                 String timeStamp = sdf.format(new Date());
81                 timeStamp = timeStamp.replace(" ", "T");
82                 timeStamp = timeStamp + "Z";
83
84                 StringBuilder sb = new StringBuilder();
85                 sb.append("Method:").append(methodName).append("\n");
86                 sb.append("Hashed Path:").append(hashedPath).append("\n");
87                 sb.append("X-Ops-Content-Hash:").append(hashedBody).append("\n");
88                 sb.append("X-Ops-Timestamp:").append(timeStamp).append("\n");
89                 sb.append("X-Ops-UserId:").append(userId);
90                 test = test + "sb " + sb + "\n";
91
92                 String auth_String = Utils.signWithRSA(sb.toString(), pemPath);
93                 String[] auth_headers = Utils.splitAs60(auth_String);
94
95                 method.addHeader("Content-type", "application/json");
96                 method.addHeader("X-Ops-Timestamp", timeStamp);
97                 method.addHeader("X-Ops-Userid", userId);
98                 method.addHeader("X-Chef-Version", "12.4.1");
99                 method.addHeader("Accept", "application/json");
100                 method.addHeader("X-Ops-Content-Hash", hashedBody);
101                 method.addHeader("X-Ops-Sign", "version=1.0");
102
103                 for (int i = 0; i < auth_headers.length; i++) {
104                         method.addHeader("X-Ops-Authorization-" + (i + 1), auth_headers[i]);
105                 }
106
107                 try{
108                 response = client.execute(method);
109                 resCode = response.getStatusLine().getStatusCode();
110                 HttpEntity entity1 = response.getEntity();
111                 responseBody = EntityUtils.toString(entity1);}
112                 catch(Exception ex){
113                         resCode=500;
114                         responseBody=ex.getMessage();
115                 }
116                 return this;
117         }
118
119         public void setHeaders(Header[] headers) {
120                 for (Header header : headers) {
121                         this.method.addHeader(header);
122                 }
123         }
124
125         public String getResponseBodyAsString() {
126                 return responseBody;
127         }
128
129         public int getReturnCode() {
130                 return resCode;
131         }
132
133         public String getReqBody() {
134                 return reqBody;
135         }
136
137         public void setReqBody(String body) {
138                 this.reqBody = body;
139         }
140
141         public String getUserId() {
142                 return userId;
143         }
144
145         public void setUserId(String userId) {
146                 this.userId = userId;
147         }
148
149         public String getPemPath() {
150                 return pemPath;
151         }
152
153         public void setPemPath(String pemPath) {
154                 this.pemPath = pemPath;
155         }
156
157         public String getChefPath() {
158                 return chefPath;
159         }
160
161         public void setChefPath(String chefPath) {
162                 this.chefPath = chefPath;
163         }
164
165         public String getOrganizations() {
166                 return organizations;
167         }
168
169         public void setOrganizations(String organizations) {
170                 this.organizations = organizations;
171         }
172 }