Skip to content

Commit

Permalink
Add support for notifying package recipients
Browse files Browse the repository at this point in the history
  • Loading branch information
SendSafely-GitHub committed Jan 16, 2020
1 parent 7e098b0 commit 336375c
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 2 deletions.
17 changes: 17 additions & 0 deletions SendSafelyAPI/src/com/sendsafely/SendSafely.java
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,23 @@ public PackageURL finalizePackage(String packageId, String keycode) throws Limit
return handler.makeRequest(packageId, keycode);
}

/**
* @description Finalizes the package so it can be delivered to the recipients.
* @param packageId The unique package id of the package to be finalized.
* @param keycode The keycode belonging to the package.
* @param notify A boolean flag indicating whether SendSafely should send the secure link to the package recipients
* @returnType PackageURL
* @return A link to access the package. This link can be sent to the recipients.
* @throws LimitExceededException
* @throws FinalizePackageFailedException
* @throws ApproverRequiredException
*/
public PackageURL finalizePackage(String packageId, String keycode, boolean notify) throws LimitExceededException, FinalizePackageFailedException, ApproverRequiredException {
FinalizePackageHandler handler = (FinalizePackageHandler)HandlerFactory.getInstance(uploadManager, Endpoint.FINALIZE_PACKAGE);
handler.setNotify(notify);
return handler.makeRequest(packageId, keycode);
}

/**
* @description Finalizes the package so it can be delivered to the recipients.
* @param packageId The packageId which is to be finalized.
Expand Down
18 changes: 18 additions & 0 deletions SendSafelyAPI/src/com/sendsafely/dto/PackageURL.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class PackageURL {
private URL secureLink;
private String keycode;
private boolean needsApproval;
private String notificationStatus = "DISABLED";

/**
* @returnType URL
Expand Down Expand Up @@ -54,6 +55,23 @@ public void setNeedsApproval(boolean needsApproval) {
this.needsApproval = needsApproval;
}

/**
* @returnType String
* @description Returns status DISABLED by default, SUCCESS if package email notification sent successfully, otherwise returns error message.
* @return notificationStatus message indicating status of package email notification
*/
public String getNotificationStatus() {
return notificationStatus;
}

/**
* @description Set internally by the API.
* @param notificationStatus the status message indication success or error
*/
public void setNotificationStatus(String notificationStatus) {
this.notificationStatus = notificationStatus;
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.sendsafely.dto.request;

import com.sendsafely.enums.GetParam;
import com.sendsafely.enums.HTTPMethod;
import com.sendsafely.json.JsonManager;

public class NotifyPackageRecipientsRequest extends BaseRequest
{

private HTTPMethod method = HTTPMethod.POST;
private String path = "/package/" + GetParam.PACKAGE_ID + "/notify/";

public NotifyPackageRecipientsRequest(JsonManager jsonManager, String keyCode) {
initialize(jsonManager, method, path);

super.setPostParam("keycode", keyCode);
}

public NotifyPackageRecipientsRequest() {
}

public void setPackageId(String packageId)
{
super.setGetParam(GetParam.PACKAGE_ID, packageId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.sendsafely.exceptions;

import java.util.List;

public class NotifyPackageRecipientsException extends Exception
{
private static final long serialVersionUID = 1L;
private List<String> errors;

String error;

public NotifyPackageRecipientsException()
{
super();
error = "unknown";
}

public NotifyPackageRecipientsException(String err){
super(err);
error = err;
}

public NotifyPackageRecipientsException(String err, List<String> errors){
super(err);
error = err;
this.errors = errors;
}

public NotifyPackageRecipientsException(Exception e){
super(e);
error = e.getMessage();
}

public String getError()
{
return error;
}

public List<String> getErrors()
{
return errors;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.sendsafely.Package;
Expand All @@ -23,6 +22,7 @@ public class FinalizePackageHandler extends BaseHandler
{

private FinalizePackageRequest request;
private boolean notify = false;

public FinalizePackageHandler(UploadManager uploadManager) {
super(uploadManager);
Expand Down Expand Up @@ -77,7 +77,18 @@ protected PackageURL makeRequest(String packageId, String packageCode, String ke

if(response.getResponse() == APIResponse.SUCCESS || response.getResponse() == APIResponse.PACKAGE_NEEDS_APPROVAL)
{
return convert(response, keyCode);
PackageURL packageUrl = convert(response, keyCode);

if (notify) {
try {
notifyPackageRecipients(packageId,keyCode);
packageUrl.setNotificationStatus(APIResponse.SUCCESS.toString());
} catch (NotifyPackageRecipientsException e) {
packageUrl.setNotificationStatus(e.getMessage());
}
}

return packageUrl;
}
else if(response.getResponse() == APIResponse.LIMIT_EXCEEDED)
{
Expand Down Expand Up @@ -154,5 +165,24 @@ protected Package convert(CreatePackageResponse obj)
info.setServerSecret(obj.getServerSecret());
return info;
}

protected void notifyPackageRecipients(String packageId, String keycode) throws NotifyPackageRecipientsException {

NotifyPackageRecipientsHandler handler = new NotifyPackageRecipientsHandler(uploadManager,keycode);

try {
handler.makeRequest(packageId);
} catch (NotifyPackageRecipientsException e) {
throw new NotifyPackageRecipientsException(e);
}
}

public boolean isNotify() {
return notify;
}

public void setNotify(boolean notify) {
this.notify = notify;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.sendsafely.handlers;

import java.io.IOException;

import com.sendsafely.dto.request.NotifyPackageRecipientsRequest;
import com.sendsafely.dto.response.BaseResponse;
import com.sendsafely.enums.APIResponse;
import com.sendsafely.exceptions.NotifyPackageRecipientsException;
import com.sendsafely.exceptions.SendFailedException;
import com.sendsafely.upload.UploadManager;

public class NotifyPackageRecipientsHandler extends BaseHandler
{

private NotifyPackageRecipientsRequest request;

public NotifyPackageRecipientsHandler(UploadManager uploadManager, String keycode) {
super(uploadManager);
this.request = new NotifyPackageRecipientsRequest(uploadManager.getJsonManager(),keycode);
}

public BaseResponse makeRequest(String packageId) throws NotifyPackageRecipientsException {
request.setPackageId(packageId);

BaseResponse response = send();

if(response.getResponse() != APIResponse.SUCCESS)
{
throw new NotifyPackageRecipientsException(response.getMessage());
}

return response;
}

protected BaseResponse send() throws NotifyPackageRecipientsException
{
try {
return send(request, new BaseResponse());
} catch (SendFailedException e) {
throw new NotifyPackageRecipientsException(e);
} catch (IOException e) {
throw new NotifyPackageRecipientsException(e);
}
}
}

0 comments on commit 336375c

Please sign in to comment.