Skip to content

Commit c48285b

Browse files
author
Maksim Markov
committed
1.18.4
1 parent 555b4b0 commit c48285b

4 files changed

Lines changed: 83 additions & 18 deletions

File tree

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ cd genesis_java
3131
<dependency>
3232
<groupId>com.emerchantpay.gateway</groupId>
3333
<artifactId>genesis-java</artifactId>
34-
<version>1.18.3</version>
34+
<version>1.18.4</version>
3535
</dependency>
3636
```
3737

@@ -162,6 +162,20 @@ Endpoints
162162

163163
The current version supports two endpoints: ```ECOMPROCESSING``` and ```EMERCHANTPAY```
164164

165+
Proxy configuration
166+
-------------------
167+
168+
Proxy configuration is optional. You can use proxy host domain name or IP address and proxy port number.
169+
170+
```java
171+
// Create proxy configuration
172+
GenesisClient client = new GenesisClient(configuration, request);
173+
client.setProxyHost("proxy_host");
174+
client.setProxyPort(8080);
175+
client.debugMode(true);
176+
client.execute();
177+
```
178+
165179
Request types
166180
-------------
167181

pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.emerchantpay.gateway</groupId>
55
<artifactId>genesis-java</artifactId>
6-
<version>1.18.3</version>
6+
<version>1.18.4</version>
77
<name>Genesis Gateway Java Client Library</name>
88
<description>Java Client Library for Genesis Gateway</description>
99
<url>https://www.emerchantpay.com</url>
@@ -32,7 +32,7 @@
3232
<developerConnection>
3333
${scmConnection}
3434
</developerConnection>
35-
<tag>1.18.3</tag>
35+
<tag>1.18.4</tag>
3636
</scm>
3737

3838
<distributionManagement>
@@ -66,22 +66,22 @@
6666
<dependency>
6767
<groupId>org.mockito</groupId>
6868
<artifactId>mockito-core</artifactId>
69-
<version>4.8.0</version>
69+
<version>4.11.0</version>
7070
</dependency>
7171
<dependency>
7272
<groupId>com.fasterxml.jackson.core</groupId>
7373
<artifactId>jackson-core</artifactId>
74-
<version>2.13.4</version>
74+
<version>2.18.0</version>
7575
</dependency>
7676
<dependency>
7777
<groupId>com.fasterxml.jackson.dataformat</groupId>
7878
<artifactId>jackson-dataformat-xml</artifactId>
79-
<version>2.13.4</version>
79+
<version>2.18.0</version>
8080
</dependency>
8181
<dependency>
8282
<groupId>org.apache.commons</groupId>
8383
<artifactId>commons-text</artifactId>
84-
<version>1.10.0</version>
84+
<version>1.12.0</version>
8585
</dependency>
8686
</dependencies>
8787

src/main/java/com/emerchantpay/gateway/GenesisClient.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public class GenesisClient extends Request {
4848

4949
private int connectTimeout = 60000;
5050
private int readTimeout = 60000;
51+
private String proxyHost;
52+
private int proxyPort;
5153

5254
// Execute
5355
private Http http;
@@ -255,6 +257,8 @@ private void execute(Request request, Configuration configuration) {
255257
http = new Http(configuration);
256258
http.setConnectTimeout(connectTimeout);
257259
http.setReadTimeout(readTimeout);
260+
http.setProxyHost(proxyHost);
261+
http.setProxyPort(proxyPort);
258262
NodeWrapper response = http.postXML(configuration.getBaseUrl(), request);
259263

260264
String requestIdentifier = getRequestIdentifier(request);
@@ -310,6 +314,29 @@ public void setReadTimeout(int timeout) {
310314
readTimeout = timeout;
311315
}
312316

317+
/**
318+
* Sets a specified proxy host value to be used when opening a communications link to the resource
319+
* referenced by this GenesisClient. Use domain or IP address.
320+
*
321+
* @param proxyHost proxy host
322+
*/
323+
public void setProxyHost(String proxyHost) {
324+
this.proxyHost = proxyHost;
325+
}
326+
327+
/**
328+
* Sets a specified proxy port value to be used when opening a communications link to the resource
329+
* referenced by this GenesisClient. A non-zero value specifies the proxy port.
330+
*
331+
* @param proxyPort proxy port
332+
*/
333+
public void setProxyPort(int proxyPort) {
334+
if (proxyPort < 1 || proxyPort > 65535) {
335+
throw new IllegalArgumentException("invalid port: " + proxyPort);
336+
}
337+
this.proxyPort = proxyPort;
338+
}
339+
313340
private Configuration getConfiguration() {
314341
Iterator<Configuration> iterator = configurationMap.values().iterator();
315342

src/main/java/com/emerchantpay/gateway/util/Http.java

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.io.StringWriter;
2727
import java.io.UnsupportedEncodingException;
2828
import java.net.HttpURLConnection;
29+
import java.net.InetSocketAddress;
30+
import java.net.Proxy;
2931
import java.net.URL;
3032
import java.net.URLDecoder;
3133
import java.net.UnknownHostException;
@@ -72,6 +74,8 @@ enum RequestMethod {
7274

7375
private int connectTimeout;
7476
private int readTimeout;
77+
private String proxyHost;
78+
private int proxyPort;
7579

7680
public Http(Configuration configuration) {
7781
this.configuration = configuration;
@@ -126,14 +130,22 @@ public NodeWrapper put(String url, Request request) {
126130
return httpRequestXML(RequestMethod.PUT, url, request.toXML());
127131
}
128132

129-
public void setConnectTimeout(int timeout){
133+
public void setConnectTimeout(int timeout) {
130134
connectTimeout = timeout;
131135
}
132136

133137
public void setReadTimeout(int timeout) {
134138
readTimeout = timeout;
135139
}
136140

141+
public void setProxyHost(String proxyHost) {
142+
this.proxyHost = proxyHost;
143+
}
144+
145+
public void setProxyPort(int proxyPort) {
146+
this.proxyPort = proxyPort;
147+
}
148+
137149
private NodeWrapper httpRequestXML(RequestMethod requestMethod, String url) {
138150
return httpRequestXML(requestMethod, url, null);
139151
}
@@ -182,28 +194,36 @@ private NodeWrapper httpRequestXML(RequestMethod requestMethod, String url, Stri
182194
responseStream = connection.getResponseCode() == 422 ? connection.getErrorStream()
183195
: connection.getInputStream();
184196

185-
String xml = StringUtils.inputStreamToString(responseStream);
197+
String responseXml = StringUtils.inputStreamToString(responseStream);
186198

187199
configuration.getLogger().log(Level.INFO, "[Genesis] [{0}]] {1} {2}",
188200
new Object[]{getCurrentTime(), requestMethod.toString(), url});
189201
configuration.getLogger().log(Level.FINE, "[Genesis] [{0}] {1} {2} {3}",
190202
new Object[]{getCurrentTime(), requestMethod.toString(), url, connection.getResponseCode()});
191203

192-
if (xml != null && configuration.isDebugModeEnabled() == true) {
204+
if (configuration.isDebugModeEnabled()) {
193205
configuration.getLogger().log(Level.INFO, formatSanitizeBodyForLog(postBody));
194-
configuration.getLogger().log(Level.INFO, formatSanitizeBodyForLog(xml));
206+
configuration.getLogger().log(Level.INFO, formatSanitizeBodyForLog(responseXml));
195207
}
196208

197-
nodeWrapper = NodeWrapperFactory.instance.create(xml);
209+
nodeWrapper = NodeWrapperFactory.instance.create(responseXml);
198210
} finally {
199211
if (responseStream != null) {
200212
responseStream.close();
201213
}
202214
}
203215
} catch (UnknownHostException e) {
204-
throw new NetworkException("No route to host " + e.getMessage() + ":" + port, e);
216+
String errorStr = "No route to host " + host + ":" + port;
217+
if (proxyHost != null && proxyPort > 0) {
218+
errorStr += " , used proxy_host: " + proxyHost + ", proxy_port: " + proxyPort;
219+
}
220+
throw new NetworkException(errorStr, e);
205221
} catch (IOException e) {
206-
throw new UnexpectedException(e.getMessage() + ", host: " + host + ", port: " + port, e);
222+
String errorStr = ", host: " + host + ", port: " + port;
223+
if (proxyHost != null && proxyPort > 0) {
224+
errorStr += ", used proxy_host: " + proxyHost + ", proxy_port: " + proxyPort;
225+
}
226+
throw new UnexpectedException(e.getMessage() + errorStr, e);
207227
} finally {
208228
if (connection != null) {
209229
connection.disconnect();
@@ -252,7 +272,7 @@ private JsonNode httpRequestJson(RequestMethod requestMethod, String url, String
252272
configuration.getLogger().log(Level.FINE, "[Genesis] [{0}] {1} {2} {3}",
253273
new Object[]{getCurrentTime(), requestMethod.toString(), url, connection.getResponseCode()});
254274

255-
if (json != null && configuration.isDebugModeEnabled() == true) {
275+
if (configuration.isDebugModeEnabled()) {
256276
configuration.getLogger().log(Level.INFO, formatSanitizeBodyForLog(postBody));
257277
configuration.getLogger().log(Level.INFO, formatSanitizeBodyForLog(json));
258278
}
@@ -338,11 +358,15 @@ private HttpURLConnection buildConnection(RequestMethod requestMethod, String ur
338358
String user_pass = configuration.getUsername() + ":" + configuration.getPassword();
339359
String encoded = Base64.getEncoder().encodeToString(user_pass.getBytes());
340360

341-
342-
connection = (HttpURLConnection) url.openConnection();
361+
if (proxyHost != null && !proxyHost.isEmpty() && proxyPort > 0) {
362+
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
363+
connection = (HttpURLConnection) url.openConnection(proxy);
364+
} else {
365+
connection = (HttpURLConnection) url.openConnection();
366+
}
343367
connection.setRequestMethod(requestMethod.toString());
344368
connection.addRequestProperty("Accept", acceptHeader);
345-
connection.setRequestProperty("Content-Type",contentType);
369+
connection.setRequestProperty("Content-Type", contentType);
346370
connection.addRequestProperty("Authorization", "Basic " + encoded);
347371
connection.setDoOutput(true);
348372
connection.setConnectTimeout(connectTimeout);

0 commit comments

Comments
 (0)