Tuesday, 16 April 2019

Upload files to SDL WorldServer using the REST API

In this article, I would like to present a method for uploading a collection of files to SDL WorldServer using the REST API.

The method takes three parameters:
  1. wsBaseUrl
  2. token
  3. files
The wsBaseUrl must be the <serverURL>:<portnumber> where your WorldServer instance is running.

The second parameter is a security token, which can be retrieved by using the SDL WorldServer REST API as explained here.

The third parameter is a Collection of File objects for upload.

The method returns a List of FileUploadResponse objects, one for each uploaded file, containing data from the uploaded asset. With this information, it is possible to create Projects in WorldServer using the REST API and place these assets through translation.

public class FileUploadResponse {
private String name;
private String internalName;
private String fullName;
private String url;
private double size;
private long creationTime;
private boolean exists;
private File[] files;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInternalName() {
return internalName;
}
public void setInternalName(String internalName) {
this.internalName = internalName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public double getSize() {
return size;
}
public void setSize(double size) {
this.size = size;
}
public long getCreationTime() {
return creationTime;
}
public void setCreationTime(long creationTime) {
this.creationTime = creationTime;
}
public boolean isExists() {
return exists;
}
public void setExists(boolean exists) {
this.exists = exists;
}
public File[] getFiles() {
return files;
}
public void setFiles(File[] files) {
this.files = files;
}
}
public static List<FileUploadResponse> uploadAssets(String wsBaseUrl, String token, Collection<File> files)
throws IOException, URISyntaxException {
List<FileUploadResponse> fileUploadResponses = new ArrayList<>();
for (File file : files) {
String boundary = "--" + UUID.randomUUID() + "--";
URI postUri = new URIBuilder(wsBaseUrl + "/ws-api/v1/files")
.addParameter("token", token)
.addParameter("content-type", "multipart/form-data; boundary=" + boundary)
.build();
MultipartEntityBuilder builder = MultipartEntityBuilder
.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.setBoundary(boundary)
.addPart("file", new FileBody(file));
HttpPost httpPost = new HttpPost(postUri);
httpPost.setEntity(builder.build());
HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse response = httpClient.execute(httpPost);
Gson gson = new GsonBuilder().create();
FileUploadResponse fileUploadResp = gson
.fromJson(new InputStreamReader(response.getEntity().getContent()), FileUploadResponse.class);
fileUploadResponses.add(fileUploadResp);
}
return fileUploadResponses;
}

No comments:

Post a Comment