|
| 1 | +import urllib |
| 2 | + |
| 3 | +from xlrelease import HttpResponse |
| 4 | + |
| 5 | +from java.lang import String |
| 6 | + |
| 7 | +from org.apache.commons.codec.binary import Base64 |
| 8 | +from org.apache.http import HttpHost |
| 9 | +from org.apache.http.client.config import RequestConfig |
| 10 | +from org.apache.http.util import EntityUtils |
| 11 | +from org.apache.http.client.methods import HttpGet, HttpPost, HttpPut, HttpDelete |
| 12 | +from org.apache.http.entity import StringEntity |
| 13 | +from org.apache.http.impl.client import HttpClients |
| 14 | +from org.apache.http.conn.ssl import SSLContextBuilder, SSLConnectionSocketFactory, TrustStrategy |
| 15 | + |
| 16 | +class TrustAllStrategy(TrustStrategy): |
| 17 | + def isTrusted(self, chain, authType): |
| 18 | + return True |
| 19 | + |
| 20 | +class HttpRequest: |
| 21 | + def __init__(self, username = None, password = None, verify = True): |
| 22 | + """ |
| 23 | + Builds an HttpRequest |
| 24 | +
|
| 25 | + :param username: the username for basic authentication |
| 26 | + (optional, no authentication will be used if empty) |
| 27 | + :param password: an password |
| 28 | + (optional) |
| 29 | + """ |
| 30 | + self.username = username |
| 31 | + self.password = password |
| 32 | + self.verify = verify |
| 33 | + |
| 34 | + def doRequest(self, **options): |
| 35 | + """ |
| 36 | + Performs an HTTP Request |
| 37 | +
|
| 38 | + :param options: A keyword arguments object with the following properties : |
| 39 | + method: the HTTP method : 'GET', 'PUT', 'POST', 'DELETE' |
| 40 | + (optional: GET will be used if empty) |
| 41 | + context: the context url |
| 42 | + (optional: the url on HttpConnection will be used if empty) |
| 43 | + body: the body of the HTTP request for PUT & POST calls |
| 44 | + (optional: an empty body will be used if empty) |
| 45 | + contentType: the content type to use |
| 46 | + (optional, no content type will be used if empty) |
| 47 | + headers: a dictionary of headers key/values |
| 48 | + (optional, no headers will be used if empty) |
| 49 | + :return: an HttpResponse instance |
| 50 | + """ |
| 51 | + request = self.buildRequest( |
| 52 | + options.get('method', 'GET'), |
| 53 | + options.get('context', ''), |
| 54 | + options.get('body', ''), |
| 55 | + options.get('contentType', None), |
| 56 | + options.get('headers', None)) |
| 57 | + |
| 58 | + return self.executeRequest(request) |
| 59 | + |
| 60 | + |
| 61 | + def get(self, context, **options): |
| 62 | + """ |
| 63 | + Performs an Http GET Request |
| 64 | +
|
| 65 | + :param context: the context url |
| 66 | + :param options: the options keyword argument described in doRequest() |
| 67 | + :return: an HttpResponse instance |
| 68 | + """ |
| 69 | + options['method'] = 'GET' |
| 70 | + options['context'] = context |
| 71 | + return self.doRequest(**options) |
| 72 | + |
| 73 | + |
| 74 | + def put(self, context, body, **options): |
| 75 | + """ |
| 76 | + Performs an Http PUT Request |
| 77 | +
|
| 78 | + :param context: the context url |
| 79 | + :param body: the body of the HTTP request |
| 80 | + :param options: the options keyword argument described in doRequest() |
| 81 | + :return: an HttpResponse instance |
| 82 | + """ |
| 83 | + options['method'] = 'PUT' |
| 84 | + options['context'] = context |
| 85 | + options['body'] = body |
| 86 | + return self.doRequest(**options) |
| 87 | + |
| 88 | + |
| 89 | + def post(self, context, body, **options): |
| 90 | + """ |
| 91 | + Performs an Http POST Request |
| 92 | +
|
| 93 | + :param context: the context url |
| 94 | + :param body: the body of the HTTP request |
| 95 | + :param options: the options keyword argument described in doRequest() |
| 96 | + :return: an HttpResponse instance |
| 97 | + """ |
| 98 | + options['method'] = 'POST' |
| 99 | + options['context'] = context |
| 100 | + options['body'] = body |
| 101 | + return self.doRequest(**options) |
| 102 | + |
| 103 | + |
| 104 | + def delete(self, context, **options): |
| 105 | + """ |
| 106 | + Performs an Http DELETE Request |
| 107 | +
|
| 108 | + :param context: the context url |
| 109 | + :param options: the options keyword argument described in doRequest() |
| 110 | + :return: an HttpResponse instance |
| 111 | + """ |
| 112 | + options['method'] = 'DELETE' |
| 113 | + options['context'] = context |
| 114 | + return self.doRequest(**options) |
| 115 | + |
| 116 | + |
| 117 | + def buildRequest(self, method, context, body, contentType, headers): |
| 118 | + |
| 119 | + method = method.upper() |
| 120 | + |
| 121 | + if method == 'GET': |
| 122 | + request = HttpGet(context) |
| 123 | + elif method == 'POST': |
| 124 | + request = HttpPost(context) |
| 125 | + request.setEntity(StringEntity(body)) |
| 126 | + elif method == 'PUT': |
| 127 | + request = HttpPut(context) |
| 128 | + request.setEntity(StringEntity(body)) |
| 129 | + elif method == 'DELETE': |
| 130 | + request = HttpDelete(context) |
| 131 | + else: |
| 132 | + raise Exception('Unsupported method: ' + method) |
| 133 | + |
| 134 | + request.addHeader('Content-Type', contentType) |
| 135 | + request.addHeader('Accept', contentType) |
| 136 | + self.setCredentials(request) |
| 137 | + self.setHeaders(request, headers) |
| 138 | + |
| 139 | + return request |
| 140 | + |
| 141 | + |
| 142 | + def setCredentials(self, request): |
| 143 | + if self.username: |
| 144 | + username = self.username |
| 145 | + password = self.password |
| 146 | + else: |
| 147 | + return |
| 148 | + |
| 149 | + encoding = Base64.encodeBase64String(String(username + ':' + password).getBytes()) |
| 150 | + request.addHeader('Authorization', 'Basic ' + encoding) |
| 151 | + |
| 152 | + |
| 153 | + def setHeaders(self, request, headers): |
| 154 | + if headers: |
| 155 | + for key in headers: |
| 156 | + request.setHeader(key, headers[key]) |
| 157 | + |
| 158 | + |
| 159 | + def executeRequest(self, request): |
| 160 | + client = None |
| 161 | + response = None |
| 162 | + try: |
| 163 | + if (self.verify): |
| 164 | + client = HttpClients.createDefault() |
| 165 | + else: |
| 166 | + client = self.createHttpClient() |
| 167 | + |
| 168 | + response = client.execute(request) |
| 169 | + status = response.getStatusLine().getStatusCode() |
| 170 | + entity = response.getEntity() |
| 171 | + result = EntityUtils.toString(entity, "UTF-8") if entity else None |
| 172 | + headers = response.getAllHeaders() |
| 173 | + EntityUtils.consume(entity) |
| 174 | + |
| 175 | + return HttpResponse.HttpResponse(status, result, headers) |
| 176 | + finally: |
| 177 | + if response: |
| 178 | + response.close() |
| 179 | + if client: |
| 180 | + client.close() |
| 181 | + |
| 182 | + |
| 183 | + def createHttpClient(self): |
| 184 | + builder = SSLContextBuilder() |
| 185 | + builder.loadTrustMaterial(None, TrustAllStrategy()) |
| 186 | + |
| 187 | + tlsVersions = ["TLSv1", "TLSv1.1", "TLSv1.2"] |
| 188 | + socketfactory = SSLConnectionSocketFactory(builder.build(), tlsVersions, None, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER) |
| 189 | + # print 'DEBUG: Created custom HttpClient to trust all certs\n' |
| 190 | + return HttpClients.custom().setSSLSocketFactory(socketfactory).build() |
0 commit comments