Skip to content

Commit

Permalink
Update to the latest publication API
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Bloom committed Jul 30, 2011
1 parent a3736c8 commit 90a07b7
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 89 deletions.
27 changes: 13 additions & 14 deletions src/main/java/com/lulu/publish/client/FileUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class FileUploader {
private static final Logger LOG = LoggerFactory.getLogger(FileUploader.class); // NOPMD

private String authenticationToken;
private String uploadToken;
private String apiKey;
private HttpClient httpClient;

/**
Expand All @@ -38,9 +38,9 @@ public class FileUploader {
* @param authenticationToken login session token
* @throws IOException if SSL context could not be initialized
*/
public FileUploader(String uploadToken, String authenticationToken) throws IOException {
public FileUploader(String authenticationToken, String apiKey) throws IOException {
this.authenticationToken = authenticationToken;
this.uploadToken = uploadToken;
this.apiKey = apiKey;

httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
Expand All @@ -55,21 +55,20 @@ public FileUploader(String uploadToken, String authenticationToken) throws IOExc
* @return true if upload is successful
* @throws IOException if upload fails because of a bad target URL or missing input files
*/
public String upload(String targetUrl, File... targetFiles) throws IOException {

HttpPost httpPost = new HttpPost(targetUrl
+ "?auth_token=" + URLEncoder.encode(authenticationToken, "UTF-8")
+ "&upload_token=" + URLEncoder.encode(uploadToken, "UTF-8"));
public String upload(String targetUrl, File file) throws IOException {
String fullURL = targetUrl
+ "?auth_token=" + URLEncoder.encode(authenticationToken, "UTF-8")
+ "&api_key=" + URLEncoder.encode(apiKey, "UTF-8");
HttpPost httpPost = new HttpPost(fullURL);
MultipartEntity multipartEntity = new MultipartEntity();
for (File file : targetFiles) {
if (LOG.isInfoEnabled()) {
LOG.info("Uploading " + file.getName() + " to " + targetUrl);
}
ContentBody fileBody = new FileBody(file);
multipartEntity.addPart(file.getName(), fileBody);
if (LOG.isInfoEnabled()) {
LOG.info("Uploading " + file.getName() + " to " + fullURL);
}
ContentBody fileBody = new FileBody(file);
multipartEntity.addPart(file.getName(), fileBody);

httpPost.setEntity(multipartEntity);
httpPost.setHeader("Accept", "application/json");
HttpResponse response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
String output = "";
Expand Down
43 changes: 12 additions & 31 deletions src/main/java/com/lulu/publish/client/PublishApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;

import org.apache.http.HttpEntity;
Expand Down Expand Up @@ -41,7 +42,6 @@
import com.lulu.publish.response.ListResponse;
import com.lulu.publish.response.ProjectResponse;
import com.lulu.publish.response.UploadResponse;
import com.lulu.publish.response.UploadTokenResponse;
import com.lulu.publish.util.JsonMapper;
import com.lulu.publish.util.JsonMapperException;
import com.lulu.publish.util.StreamUtils;
Expand All @@ -61,7 +61,8 @@ public class PublishApiClient {
private String password;
private String authenticationEndpoint = "https://www.lulu.com/account/endpoints/authenticator.php";
private String apiUrlTemplate = "https://apps.lulu.com/api/publish/v1/%s";
private String apiUploadUrl = "https://pubapp.lulu.com/api/publish/v1/upload";
private String apiConvertUrlTemplate = "https://apps.lulu.com/api/create/v1/%s";
private String apiUploadUrl = "https://transfer.lulu.com/api/create/v1/file";
private String authenticationToken;

private ErrorResponse error;
Expand Down Expand Up @@ -298,10 +299,12 @@ public void delete(int contentId) throws PublishApiException {
* @throws PublishApiException if an unexpected error occurs
* @return uploaded files
*/
public UploadResponse upload(Collection<File> files) throws PublishApiException, FileNotFoundException {
String uploadToken = requestUploadToken();
public UploadResponse upload(File file) throws PublishApiException, FileNotFoundException {
try {
return performUpload(files, uploadToken);
FileUploader uploader = new FileUploader(authenticationToken, apiKey);
List<UploadResponse> responses = new ArrayList<UploadResponse>();
String response = uploader.upload(apiUploadUrl, file);
return JsonMapper.fromJson(response, UploadResponse.class);
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
Expand All @@ -311,38 +314,16 @@ public UploadResponse upload(Collection<File> files) throws PublishApiException,
}
}

private UploadResponse performUpload(Collection<File> files, String uploadToken) throws IOException, JsonMapperException {
FileUploader uploader = new FileUploader(uploadToken, authenticationToken);
String response = uploader.upload(apiUploadUrl, files.toArray(new File[files.size()]));
return JsonMapper.fromJson(response, UploadResponse.class);
}

private String requestUploadToken() throws PublishApiException {
assertAuthenticated();

ApiResponse response = performApiCall(String.format(apiUrlTemplate, "request_upload_token"),
generateParameters("api_key", apiKey, "auth_token", authenticationToken),
UploadTokenResponse.class);

if (response.isError()) {
error = (ErrorResponse) response.getPayload();
throw new PublishApiException("Failed to obtain file upload token.");
}
return ((UploadTokenResponse) response.getPayload()).getToken();
}

/**
* Download the indicated project file.
*
* @param contentId project identifier
* @param fileContext which file to download
* @param outputLocation where to put the file
* @throws PublishApiException if an unexpected error occurs
*/
public void download(int contentId, FileContext fileContext, File outputLocation) throws PublishApiException {
public void download(long fileId, File outputLocation) throws PublishApiException {
assertAuthenticated();

String downloadUrl = String.format(apiUrlTemplate, "download/id/" + Integer.toString(contentId) + "/what/" + fileContext.toString())
String downloadUrl = apiUploadUrl + "/" + fileId + "/data"
+ "?" + URLEncodedUtils.format(generateParameters("api_key", apiKey, "auth_token", authenticationToken), "UTF-8");
if (LOG.isInfoEnabled()) {
LOG.info("API call to <{}>", downloadUrl);
Expand Down Expand Up @@ -375,7 +356,7 @@ public void download(int contentId, FileContext fileContext, File outputLocation
public Long convert(ConversionManifest manifest) throws PublishApiException {
assertAuthenticated();

String call = String.format(apiUrlTemplate, "conversion")
String call = String.format(apiConvertUrlTemplate, "conversion")
+ "?" + URLEncodedUtils.format(generateParameters("api_key", apiKey, "auth_token", authenticationToken), "UTF-8");
if (LOG.isInfoEnabled()) {
LOG.info("API call to <{}>", call);
Expand Down Expand Up @@ -433,7 +414,7 @@ private String serializeManifest(ConversionManifest manifest) throws PublishApiE
*/
public Conversion convertStatus(Long jobId) throws PublishApiException {

String call = String.format(apiUrlTemplate, String.format("conversion/%d", jobId))
String call = String.format(apiConvertUrlTemplate, String.format("conversion/%d", jobId))
+ "?" + URLEncodedUtils.format(generateParameters("api_key", apiKey, "auth_token", authenticationToken), "UTF-8");

if (LOG.isInfoEnabled()) {
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/lulu/publish/model/Callback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.lulu.publish.model;

public class Callback {
private String url;
private String mimeType;

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getMimeType() {
return mimeType;
}

public void setMimeType(String mimeType) {
this.mimeType = mimeType;
}
}
1 change: 1 addition & 0 deletions src/main/java/com/lulu/publish/model/ConversionFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public class ConversionFile {

public static final String MIME_TYPE_PDF = "application/pdf";
public static final String MIME_TYPE_EPUB = "application/epub+zip";

private Long conversionFileId;
private URI uri;
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/com/lulu/publish/model/ConversionManifest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import java.util.ArrayList;
import java.util.Collection;

import org.codehaus.jackson.annotate.JsonProperty;

public class ConversionManifest {

private String outputFormat;
private Dimensions outputDimensions;
private String callbackUrl;
private Callback callback;
private Collection<ConversionFile> conversionFiles;

/**
Expand All @@ -34,12 +36,14 @@ public void setOutputDimensions(Dimensions outputDimensions) {
this.outputDimensions = outputDimensions;
}

public String getCallbackUrl() {
return callbackUrl;
@JsonProperty("callback")
public Callback getCallback() {
return callback;
}

public void setCallbackUrl(String callbackUrl) {
this.callbackUrl = callbackUrl;
@JsonProperty("callback")
public void setCallbackUrl(Callback callback) {
this.callback = callback;
}

public Collection<ConversionFile> getConversionFiles() {
Expand All @@ -57,7 +61,7 @@ public void addConversionFiles(ConversionFile jobFile) {
@Override
public String toString() {
return "ConversionManifest {"
+ "callbackUrl=\'" + callbackUrl
+ "callbackUrl=\'" + callback.getUrl()
+ "\', conversionFiles=\'" + conversionFiles
+ "\', outputDimensions=\'" + outputDimensions
+ "\', outputFormat=\'" + outputFormat
Expand Down
38 changes: 0 additions & 38 deletions src/main/java/com/lulu/publish/model/LuluFile.java

This file was deleted.

0 comments on commit 90a07b7

Please sign in to comment.