All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sendsafely.handlers.GetPackagesHandler Maven / Gradle / Ivy

package com.sendsafely.handlers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.sendsafely.dto.Confirmation;
import com.sendsafely.dto.request.GetPackagesRequest;
import com.sendsafely.dto.response.ConfirmationResponse;
import com.sendsafely.dto.response.FileResponse;
import com.sendsafely.dto.response.GetPackagesResponse;
import com.sendsafely.dto.response.PackageInformationResponse;
import com.sendsafely.dto.response.PackageListResponse;
import com.sendsafely.dto.response.RecipientResponse;
import com.sendsafely.enums.APIResponse;
import com.sendsafely.exceptions.GetPackagesException;
import com.sendsafely.exceptions.SendFailedException;
import com.sendsafely.upload.UploadManager;
import com.sendsafely.File;
import com.sendsafely.Package;
import com.sendsafely.PackageReference;
import com.sendsafely.Recipient;

public class GetPackagesHandler extends BaseHandler 
{	
	
	private GetPackagesRequest request;
	
	public GetPackagesHandler(UploadManager uploadManager, GetPackagesRequest request) {
		super(uploadManager);
		
		this.request = request;
	}

	public List makeRequest() throws GetPackagesException {
		GetPackagesResponse response = send();
		
		if(response.getResponse() == APIResponse.SUCCESS) 
		{
			return convert(response.getPackages());
		}
		else
		{
			throw new GetPackagesException(response.getMessage());
		}
	}
	
	protected List convert(List rawPackages)
	{
		List packages = new ArrayList(rawPackages.size());
		for(PackageListResponse resp : rawPackages) {
			packages.add(convert(resp));
		}
		return packages;
	}
	
	protected PackageReference convert(PackageListResponse obj) {
		PackageReference info = new PackageReference();
		info.setApproverList(obj.getApproverList());
		info.setFiles(convertFiles(obj.getFiles()));
		info.setLife(obj.getLife());
		info.setNeedsApproval(obj.getNeedsApproval());
		info.setPackageCode(obj.getPackageCode());
		info.setPackageId(obj.getPackageId());
		info.setRecipients(obj.getRecipients());
		info.setContactGroupNames(obj.getContactGroups());
		info.setServerSecret(obj.getServerSecret());
		info.setState(obj.getState());
		info.setPackageTimestamp(obj.getPackageUpdateTimestamp());
		info.setPackageOwner(obj.getPackageUserName());
		info.setPackageParentId(obj.getPackageParentId());
		
		return info;
	}

	protected List convertFiles(List responses) 
	{
		if(responses == null) {
			return new ArrayList();
		}
		
		List retval = new ArrayList(responses.size());
		for (FileResponse resp : responses)
		{
			retval.add(createFile(resp));
		}
		
		return retval;
	}
	
	protected File createFile(FileResponse resp)
	{
		File f = new File();
		f.setFileId(resp.getFileId());
		f.setFileName(resp.getFileName());
		f.setFileSize(resp.getFileSize());
		//f.setCreatedBy(resp.getCreatedByEmail());
		return f;
	}
	
	protected List createConfirmationList(RecipientResponse rr)
	{
		List retval = new ArrayList(rr.getConfirmations().size());
		for(ConfirmationResponse cr : rr.getConfirmations())
		{
			Confirmation c = new Confirmation();
			c.setIpAddress(cr.getIpAddress());
			c.setTimestamp(cr.getTimestamp());
			c.setFile(createFile(cr.getFile()));
			retval.add(c);
		}
		
		return retval;
	}
	
	protected GetPackagesResponse send() throws GetPackagesException
	{
		try {
			return send(request, new GetPackagesResponse());
		} catch (SendFailedException e) {
			throw new GetPackagesException(e);
		} catch (IOException e) {
			throw new GetPackagesException(e);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy