Extensis Logo SUPPORT | FORUMS | KNOWLEDGE BASE

Tracking jobs


#1

Hello it’s me again.

We are currently developing java code to achieve the following process:

  1. Manipulation process of the asset to convert the image; then
  2. Store the manipulated image in a temporary folder <port_<date_time_stamp>. folder name is generated by the script; then
  3. Then at completion of process 2, the files are ftp to an ftp server.

We are having issue with process 2. We use the job task code sample as a reference, but unable to figure out how to save the image directly to the temporary folders without going through the showjobs > download in the webclient.

Are you able to assist?

Thank you.

Alex


#2

[quote=“vertu”]Hello it’s me again.

We are having issue with process 2. We use the job task code sample as a reference, but unable to figure out how to save the image directly to the temporary folders without going through the showjobs > download in the webclient.
[/quote]
Hi Alex,

Here is a code sample that may be able to help you with your project. Basically, this code checks the currently existing Jobs and prompts the user if they want to download them. If the user chooses yes, then it will download the file to a predetermined location.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.RSAPublicKeySpec;
import java.util.List;

import javax.crypto.Cipher;
import javax.xml.namespace.QName;

import extensis.portfolio.service.asset.AssetFault;
import extensis.portfolio.service.asset.AssetSEI;
import extensis.portfolio.service.asset.AssetSEIService;
import extensis.portfolio.service.asset.JobStatus;
import extensis.portfolio.service.asset.KeySpecification;
import extensis.portfolio.service.asset.Attribute;

public class downloadJob 
{
	private String SERVER;
	private String PORT;
	private String DOWNLOADFOLDER;
	private String sessionId;
	
	public void main(String[] args) 
	throws Exception 
	{
		//Create some variables
		final String username = "administrator";
		final String passwd = "password";
		this.SERVER = "127.0.0.1";
		this.PORT = "8090";
		this.DOWNLOADFOLDER = "/Volumes/HardDrive/Downloads/";
		
		//Create the service
		URL serviceWsdlURL = new URL("http://" + SERVER + ":" + PORT + "/ws/1.0/AssetService?wsdl");
		QName serviceQName = new QName("http://portfolio.extensis/service/asset", "AssetSEIService");
		AssetSEI service = new AssetSEIService(serviceWsdlURL, serviceQName).getAssetSEIPort();
		
		//log into the Portfolio Server
		KeySpecification ks = service.getRSAPublicEncryptionKey();
		String epasswd = encryptPasswordForKeySpec(ks, passwd);
		this.sessionId = service.login(username, epasswd);
		
		//Create input buffer.
		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
		
		//Get a list of the existing Jobs and their Status.
		List<String> myJobIds = service.getJobIDs(sessionId);	
		List<JobStatus> statuses = service.getStatusForJobs(sessionId, myJobIds);
		if (statuses.size() > 0 ) {
			
			for ( JobStatus perStatus : statuses ) {

				printRecord("downloadFilename",perStatus);
				System.out.println("Status: " + perStatus.getStatus() );
				printRecord("totalFiles",perStatus);

				//Gets the location of the finished file.
				URL download = new URL("http://" + SERVER + ":" + PORT + printField("downloadLocation", perStatus) + "&sessionId=" + sessionId);
				System.out.println(download.toString());

				System.out.print("Do you want to download this file?: ");
				String response = input.readLine();
				response = response.toLowerCase();
				if (response.equals("yes") || response.equals("y")) {
					//Download the file.
					downloadFile(download, printField("downloadFilename", perStatus));
					System.out.println("File has been downloaded.");
				} else {
					System.out.println("File was not downloaded.  Next file.");
					System.out.println("");
				}
			}
		}
		
		//Log out when done.
		takeBreak(input, null);
		System.out.println("Logging Out");
		service.logout(sessionId);
		
	} // close main
	
	// Utilities ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	
	public void downloadFile(URL website, String filename) throws FileNotFoundException {
		ReadableByteChannel rbc = null;
		if (filename.equals("")) {
			filename = "files.zip";
		}
		try {
			rbc = Channels.newChannel(website.openStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	    FileOutputStream fos = new FileOutputStream(DOWNLOADFOLDER + filename);
	    try {
			fos.getChannel().transferFrom(rbc, 0, 1 << 24);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void printRecord( String name, JobStatus status) {
		System.out.println(name + ": " + printField(name, status));
	}
	
	public static String printField( String name, JobStatus status) {
		List<Attribute> fields = status.getJobDetails();
		String result = "";
		for (Attribute att : fields) {
			if (att.getName().equals(name)) {
				result = att.getValue();
			}
		}
		return result;
	}
	
    public static void reason(AssetFault err)
    {
        System.out.println("PROCESS FAILED - ");
        String reason = err.getMessage();
        System.out.println("Reason: " + reason);
        String source = err.getDetails();
        System.out.println("Source: " + source);
    }
	
	public static void takeBreak( BufferedReader inp, String str ) {
		try {
			if ( str != null) {
				System.out.println(str);
			}
			System.out.print("Hit any key to continue:");
			inp.readLine();
		} catch ( IOException e ) {
			System.out.println("IOException: " + e);
		}
	}
	
	//Methods used for encrypting passwords for login.
	public static String encryptPasswordForKeySpec(KeySpecification ksd, String password) throws Exception {
		RSAPublicKeySpec keySpec = new RSAPublicKeySpec(new BigInteger(ksd.getModulusBase16(), 16), new BigInteger(ksd.getExponent()));
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		PublicKey pk = keyFactory.generatePublic(keySpec);
		return Base64.encodeBytes(encrypt(pk, password.getBytes()));
	} //close method
	
	private static byte[] encrypt(PublicKey pk, byte[] src) {
		try {
			Cipher cipher = Cipher.getInstance("RSA");
			cipher.init(Cipher.ENCRYPT_MODE, pk);
			return cipher.doFinal(src);
		} catch (Exception e) {
			throw new RuntimeException("error encrypting cipher data: ", e);
		}
	} // close method
}//close class

Please let me know if you have any questions about this code.
-Matt