-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDfsUploadClient.java
More file actions
154 lines (138 loc) · 5.71 KB
/
DfsUploadClient.java
File metadata and controls
154 lines (138 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package dfstudio.api.upload.client;
import dfstudio.http.HttpClient;
import dfstudio.http.HttpResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* This class uploads files into DF Studio. This client does not do retry, the HttpClient should attempt this.
*
* <pre>
* //This is just an interface with only two methods, feel free to implement your own
* HttpClient http = new JavaHttpClient();
*
* String dfstudioUrl = "https://#BRAND#.dfstudio.com"; //the root url of the dfstudio cluster you sign into
* String account = "ACCOUNT"; //short account name
* String username = "USERNAME"; //username/login name
* String password = "PASSWORD";
* DfsUploadClient client = DfsUploadClient.newUpload(http, dfstudioUrl, account, username, password);
*
* String folder = "landscapes"; //optional, can be empty or null, project will be in root folder
* String project = "highlands";
* String setup = "walking path"; //optional, can be empty or null
* String filename = "p1001-mountain-trail.jpg";
* InputStream stream = getClass().getResourceAsStream(filename);
*
* client.uploadFile(folder,project,setup,filename,stream);
* </pre>
*
*
*
* @author msgile
* @author $LastChangedBy$
* @version $Revision$ $LastChangedDate$
* @since 2/26/15
*/
public class DfsUploadClient {
/**
*
* @param client Required {@see JavaHttpClient}
* @param dfstudioUrl Required
* @param accountShortName Required
* @param username Required
* @param password Required, plain text password
*/
public static DfsUploadClient newUpload(HttpClient client, String dfstudioUrl, String accountShortName, String username, String password) {
Map<String,String> authenticationData = new HashMap<String,String>(6);
authenticationData.put("account", accountShortName);
authenticationData.put("username",username);
authenticationData.put("password",password);
return new DfsUploadClient(client, dfstudioUrl,authenticationData);
}
private final HttpClient client;
private final String baseUrl;
private final Map<String,String> authenticationData;
private volatile String authenticateUrl = null;
private final Object lock = new Object();
public DfsUploadClient(HttpClient client, String baseUrl, Map<String, String> authenticationData) {
this.client = client;
this.baseUrl = baseUrl;
this.authenticationData = authenticationData;
}
private String getAuthenticateUrl() throws IOException {
if ( authenticateUrl == null ) {
synchronized (lock) {
if ( authenticateUrl == null ) {
HttpResponse response = client.post(baseUrl+"/rest/v3/session.json", authenticationData);
if ( response.getStatusCode() != 200 ) {
throw new IOException("Unable to Authenticate\nCode="+response.getStatusCode()+"\nMessage="+response.getMessageAsString() );
}
authenticateUrl = response.getMessageAsString();
int pos = authenticateUrl.indexOf(".json");
authenticateUrl = authenticateUrl.substring(1,pos);
}
}
}
return authenticateUrl;
}
public void uploadFile(String folder, String job, String setup, File file) throws IOException {
uploadFile(folder,job,setup,file.getName(),new FileInputStream(file));
}
public void uploadFile(String folder, String job, String setup, String filename, InputStream source) throws IOException {
String path;
if ( folder!=null && !folder.isEmpty() ) {
path = folder+"/"+job+"/"+filename;
} else {
path = job+"/"+filename;
}
uploadFile(path,setup,source);
}
public void uploadFile(String path, String setup, InputStream source) throws IOException {
String authenticateUrl = getAuthenticateUrl();
//first, issue an upload request with the image and setup information
String imageUploadRequestUrl = String.format("%s/path/%s", authenticateUrl, path);
Map<String,String> parameters = new HashMap<String,String>(3);
if ( setup != null && ! setup.isEmpty()) {
parameters.put("setup", setup);
}
parameters.put("action","uploadurlrequest");
parameters.put("type","image");
HttpResponse response = client.post(imageUploadRequestUrl, parameters);
validateResponse(response);
//get the upload url, content type and callback URL returned from the upload request
String responseMessage = response.getMessageAsString();
String uploadUrl = extractJsonMapValue(responseMessage, "uploadUrl");
String contentType = extractJsonMapValue(responseMessage, "uploadContentType");
String callbackUrl = extractJsonMapValue(responseMessage, "uploadOnCompleteUrl");
//upload the image
response = client.put(uploadUrl, contentType, source);
validateResponse(response);
//after the image has been uploaded, post to the callback url to notify of the successfully completed upload
response = client.post(callbackUrl, new HashMap<String,String>());
validateResponse(response);
}
public void validateResponse(HttpResponse response) throws IOException {
int code = response.getStatusCode();
if ( 300 <= code ) {
if ( 408 == code ) {
//session expired
authenticateUrl = null;
}
throw new IOException("Status Code:"+code+"\n"+response.getMessageAsString());
}
}
public static String extractJsonMapValue(String json, String key) {
Pattern pattern = Pattern.compile(String.format("\"%s\"\\s*:\\s*\"([^\"]+)\"",key));
Matcher matcher = pattern.matcher(json);
if ( matcher.find() ) {
return matcher.group(1);
} else {
return "";
}
}
}