Skip to content

Commit 64ecbaa

Browse files
author
Aman Alam
committed
Merge branch 'master' of https://github.com/wyvern8/NetStorageKit-Java into wyvern8-master
2 parents e948810 + 1640cdd commit 64ecbaa

File tree

5 files changed

+102
-12
lines changed

5 files changed

+102
-12
lines changed

src/com/akamai/netstorage/NetStorage.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,20 @@ public class NetStorage {
4444

4545
private DefaultCredential credential;
4646

47+
// defaults
48+
private int connectTimeout = 10000;
49+
private int readTimeout = 10000;
50+
4751
public NetStorage(DefaultCredential credential) {
4852
this.credential = credential;
4953
}
5054

55+
public NetStorage(DefaultCredential credential, int connectTimeout, int readTimeout) {
56+
this.credential = credential;
57+
if (connectTimeout > 0) this.setConnectTimeout(connectTimeout);
58+
if (readTimeout > 0) this.setReadTimeout(readTimeout);
59+
}
60+
5161
protected URL getNetstorageUri(String path) {
5262
try {
5363
if (!path.startsWith("/")) path = "/" + path;
@@ -60,9 +70,18 @@ protected URL getNetstorageUri(String path) {
6070

6171
protected InputStream execute(String method, String path, APIEventBean acsParams, InputStream uploadStream, Long size) throws NetStorageException {
6272
try {
73+
return new NetStorageCMSv35Signer(
74+
method,
75+
this.getNetstorageUri(path),
76+
acsParams,
77+
uploadStream,
78+
size != null && size > 0 ? size : -1,
79+
this.getConnectTimeout(),
80+
this.getReadTimeout()
81+
).execute(this.credential);
82+
}
83+
catch (RequestSigningException ex) {
6384
return createRequestSigner(method, path, acsParams, uploadStream, size).execute(this.credential);
64-
} catch (RequestSigningException ex) {
65-
throw new NetStorageException(ex);
6685
}
6786
}
6887

@@ -257,4 +276,20 @@ public boolean setmd(String path, Map<String, String> additionalParams) throws N
257276
return true;
258277
}
259278

279+
public void setConnectTimeout(int connectTimeout) {
280+
this.connectTimeout = connectTimeout;
281+
}
282+
283+
public int getConnectTimeout() {
284+
return connectTimeout;
285+
}
286+
287+
public void setReadTimeout(int readTimeout) {
288+
this.readTimeout = readTimeout;
289+
}
290+
291+
public int getReadTimeout() {
292+
return readTimeout;
293+
}
294+
260295
}

src/com/akamai/netstorage/NetStorageCMSv35Signer.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public class NetStorageCMSv35Signer implements RequestSigner {
5656
private static final String AUTH_DATA_HEADER = "X-Akamai-ACS-Auth-Data";
5757
private static final String AUTH_SIGN_HEADER = "X-Akamai-ACS-Auth-Sign";
5858

59+
// defaults
60+
private int connectTimeout = 10000;
61+
private int readTimeout = 10000;
62+
5963
/**
6064
* There are multiple types of Net Storage. The following types are
6165
* detected:
@@ -106,7 +110,7 @@ public KeyedHashAlgorithm getAlgorithm() {
106110
* @param params the set of bean parameters to be sent in the API request
107111
*/
108112
public NetStorageCMSv35Signer(String method, URL url, APIEventBean params) {
109-
this(method, url, params, null, -1L);
113+
this(method, url, params, null, -1L, -1, -1);
110114
}
111115

112116
/**
@@ -121,13 +125,15 @@ public NetStorageCMSv35Signer(String method, URL url, APIEventBean params) {
121125
* @param uploadStream the inputStream to read the bytes to upload
122126
* @param uploadSize if available, the total size of the inputStream. Note, that this could be different than the Size parameter in the action line
123127
*/
124-
public NetStorageCMSv35Signer(String method, URL url, APIEventBean params, InputStream uploadStream, long uploadSize) {
128+
public NetStorageCMSv35Signer(String method, URL url, APIEventBean params, InputStream uploadStream, long uploadSize, int connectTimeout, int readTimeout) {
125129
this.setMethod(method);
126130
this.setUrl(url);
127131
this.setParams(params);
128132
this.setUploadStream(uploadStream);
129133
this.setUploadSize(uploadSize);
130134
this.setSignVersion(SignType.HMACSHA256);
135+
if (connectTimeout > 0) this.setConnectTimeout(connectTimeout);
136+
if (readTimeout > 0) this.setReadTimeout(readTimeout);
131137
}
132138

133139
private String method;
@@ -185,6 +191,22 @@ public void setSignVersion(SignType signVersion) {
185191
this.signVersion = signVersion;
186192
}
187193

194+
public void setConnectTimeout(int connectTimeout) {
195+
this.connectTimeout = connectTimeout;
196+
}
197+
198+
public int getConnectTimeout() {
199+
return connectTimeout;
200+
}
201+
202+
public void setReadTimeout(int readTimeout) {
203+
this.readTimeout = readTimeout;
204+
}
205+
206+
public int getReadTimeout() {
207+
return readTimeout;
208+
}
209+
188210
/**
189211
* Computes the value for the the X-Akamai-ACS-Action: header. This is a url query-string encoded separated
190212
* list of parameters in the form of name=value&amp;name2=value2. For extensibility purposes, we use generic method
@@ -273,7 +295,8 @@ public boolean validate(HttpURLConnection connection) throws NetStorageException
273295
// Validate Server-Time drift
274296
Date currentDate = new Date();
275297
long responseDate = connection.getHeaderFieldDate("Date", 0);
276-
if (responseDate != 0 && currentDate.getTime() - responseDate > 30 * 1000)
298+
if ((responseDate != 0 && currentDate.getTime() - responseDate > 30 * 1000)
299+
|| (responseDate != 0 && (currentDate.getTime() - responseDate) * -1 > 30 * 1000))
277300
throw new NetStorageException("Local server Date is more than 30s out of sync with Remote server");
278301

279302
// generic response
@@ -314,8 +337,8 @@ public HttpURLConnection sign(HttpURLConnection request, ClientCredential creden
314337
public InputStream execute(HttpURLConnection request, ClientCredential credential) throws RequestSigningException {
315338
try {
316339
request = sign(request, credential);
317-
request.setConnectTimeout(10000);
318-
request.setReadTimeout(10000);
340+
request.setConnectTimeout(this.getConnectTimeout());
341+
request.setReadTimeout(this.getReadTimeout());
319342

320343
if (this.getMethod().equals("PUT") || this.getMethod().equals("POST")) {
321344
request.setDoOutput(true);

src/com/akamai/netstorage/cli/CMS.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.InputStream;
2222
import java.io.OutputStream;
2323
import java.util.Properties;
24+
import java.lang.Integer;
2425

2526
import com.akamai.netstorage.DefaultCredential;
2627
import com.akamai.netstorage.NetStorage;
@@ -51,6 +52,8 @@ public static void main(String[] args) throws Exception {
5152
String outputfile = null;
5253
String targetFilename = null;
5354
String dstFilename = null;
55+
int connectTimeout = 10000;
56+
int readTimeout = 10000;
5457
boolean indexZip = false;
5558

5659

@@ -82,6 +85,11 @@ public static void main(String[] args) throws Exception {
8285
case "-d":
8386
dstFilename = arg;
8487
break;
88+
case "-c":
89+
connectTimeout = Integer.parseInt(arg);
90+
case "-r":
91+
readTimeout = Integer.parseInt(arg);
92+
break;
8593
}
8694
firstarg = null;
8795
} else if (arg.equals("-indexzip"))
@@ -90,11 +98,18 @@ else if (!arg.startsWith("-"))
9098
netstorageURI = arg;
9199
else
92100
firstarg = arg;
93-
execute(action, user, key, netstorageURI, uploadfile, outputfile, targetFilename, dstFilename, indexZip);
101+
102+
try {
103+
execute(action, user, key, netstorageURI, uploadfile, outputfile, targetFilename, dstFilename, indexZip, connectTimeout,readTimeout);
104+
} catch (NetStorageException e) {
105+
System.out.println(e.getMessage());
106+
throw e;
107+
}
94108
}
95109

96110
public static void execute(String action, String user, String key, String netstorageURI,
97-
String uploadfile, String outputfile, String target, String dst, boolean indexZip) throws NetStorageException, IOException {
111+
String uploadfile, String outputfile, String target, String dst, boolean indexZip,
112+
int connectTimeout, int readTimeout) throws NetStorageException, IOException {
98113

99114
String host = null;
100115
String path = netstorageURI;
@@ -118,7 +133,7 @@ public static void execute(String action, String user, String key, String netsto
118133
host = hostpath[0];
119134
path = "/" + hostpath[1];
120135
}
121-
NetStorage ns = new NetStorage(new DefaultCredential(host, user, key));
136+
NetStorage ns = new NetStorage(new DefaultCredential(host, user, key), connectTimeout, readTimeout);
122137
InputStream result = null;
123138
boolean success = true;
124139

@@ -211,6 +226,7 @@ static void help()
211226
+ "Usage: cms <-a action> <-u user> <-k key>\n"
212227
+ "[-o outfile] [-f srcfile]\n"
213228
+ "[-t targetpath] [-d newpath]\n"
229+
+ "[-c connectTimeout] [-r readTimeout]\n"
214230
+ "<-indexzip> <host/path>\n"
215231
+ "\n"
216232
+ "Where:\n"
@@ -219,6 +235,8 @@ static void help()
219235
+ "key unique key used to sign api requests\n"
220236
+ "outfile local file name to write when action=download\n"
221237
+ "srcfile local file used as source when action=upload\n"
238+
+ "connectTimeout http connect timeout in milliseconds\n"
239+
+ "readTimeout http read timeout in milliseconds - useful when uploading via proxy\n"
222240
+ "targetpath the absolute path (/1234/example.jpg) pointing to the existing target when action=symlink\n"
223241
+ "newpath the absolute path (/1234/example.jpg) for the new file when action=rename\n"
224242
+ "host/path the netstorage hostname and path to the file being manipulated (example.akamaihd.net/1234/example.jpg)\n"

test/com/akamai/netstorage/NetStorageCMSv35SignerTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public void testValidateServerDateSync() throws Exception {
159159
}
160160

161161
@Test
162-
public void testValidateServerDateOutOfSync() throws Exception {
162+
public void testValidateServerDateOutOfSyncPast() throws Exception {
163163
exception.expect(NetStorageException.class);
164164
exception.expectMessage("Local server Date is more than 30s out of sync with Remote server");
165165
NetStorageCMSv35Signer netStorageCMSv35Signer = createAPIConnection();
@@ -169,6 +169,17 @@ public void testValidateServerDateOutOfSync() throws Exception {
169169
assertTrue(netStorageCMSv35Signer.validate(httpURLConnection));
170170
}
171171

172+
@Test
173+
public void testValidateServerDateOutOfSyncFuture() throws Exception {
174+
exception.expect(NetStorageException.class);
175+
exception.expectMessage("Local server Date is more than 30s out of sync with Remote server");
176+
NetStorageCMSv35Signer netStorageCMSv35Signer = createAPIConnection();
177+
HttpURLConnectionTest httpURLConnection = new HttpURLConnectionTest(netStorageCMSv35Signer.getUrl());
178+
httpURLConnection.setResponseCode(HttpURLConnection.HTTP_UNAVAILABLE);
179+
httpURLConnection.getHeaderFields().put("Date", Collections.singletonList("Wed, 1 Jan 3000 11:00:00 GMT"));
180+
assertTrue(netStorageCMSv35Signer.validate(httpURLConnection));
181+
}
182+
172183
@Test
173184
public void testExecuteOK() throws Exception {
174185
NetStorageCMSv35Signer netStorageCMSv35Signer = createAPIConnection();

test/com/akamai/netstorage/NetStorageTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ private NetStorage createNetstorage(String path) {
4646

4747
URLStreamHandlerFactoryTest.init();
4848

49-
NetStorage ns = new NetStorage(new DefaultCredential("www.example.com", "user1", "secret1"));
49+
NetStorage ns = new NetStorage(new DefaultCredential("www.example.com", "user1", "secret1"), 20000, 20000);
50+
51+
assertEquals(ns.getConnectTimeout(), 20000);
52+
assertEquals(ns.getReadTimeout(), 20000);
5053

5154
HttpURLConnectionTest connection = URLStreamHandlerFactoryTest.addURLConnection(ns.getNetstorageUri(path));
5255
connection.setResponseCode(HttpURLConnection.HTTP_OK);

0 commit comments

Comments
 (0)