/* * * IBM and Sterling Commerce Confidential * * OCO Source Materials * * IBM B2B Sterling Integrator * * (c) Copyright Sterling Commerce, an IBM Company 2001, 2011 * * The source code for this program is not published or otherwise * divested of its trade secrets, irrespective of what has been * deposited with the U.S. Copyright Office. * */ package com.sterlingcommerce.fg.services;
import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.SQLException; import java.util.List;
import org.openstack4j.api.OSClient; import org.openstack4j.model.common.ActionResponse; import org.openstack4j.model.common.DLPayload; import org.openstack4j.model.common.Identifier; import org.openstack4j.model.common.Payloads; import org.openstack4j.model.storage.object.SwiftContainer; import org.openstack4j.model.storage.object.SwiftObject; import org.openstack4j.openstack.OSFactory;
import com.sterlingcommerce.woodstock.mailbox.MailboxException; import com.sterlingcommerce.woodstock.mailbox.MailboxFactory; import com.sterlingcommerce.woodstock.mailbox.repository.IRepository; import com.sterlingcommerce.woodstock.mailbox.repository.Message; import com.sterlingcommerce.woodstock.services.IService; import com.sterlingcommerce.woodstock.util.frame.Manager; import com.sterlingcommerce.woodstock.util.frame.log.LogService;
import com.sterlingcommerce.woodstock.util.frame.log.Logger; import com.sterlingcommerce.woodstock.workflow.Document; import com.sterlingcommerce.woodstock.workflow.DocumentNotFoundException; import com.sterlingcommerce.woodstock.workflow.WorkFlowContext; import com.sterlingcommerce.woodstock.workflow.WorkFlowException;
import jarjar.orgapachecommonsio.IOUtils;
/** * The Reroute Service is started as part of a business process. * * It takes no input. * * @author Bipin Chandra */ public class IBMCloudObjectStorageService implements IService {
private static final String CLASS_NAME = "IBMCloudObjectStorageService";
private Document primaryDoc = null;
private InputStream docAsStream = null;
private static boolean isOK = true;
protected static Logger log; static { log = LogService.getLogger("integrationservices"); } /** * This method is entered when a Service is invoked during a business * process. * <P> * * @return Updated WorkFlowContext after Service processes */ public WorkFlowContext processData(final WorkFlowContext wfc) throws WorkFlowException { final String METHOD = ".processData() - "; if (log.isDebug()) log.log(CLASS_NAME + METHOD + "Starting ...");
isOK = true;
//Hard /*String auth_url= "https://identity.open.softlayer.com"+ "/v3"; String project= "object_storage_92180e99_fd78_438b_860f_d8f9dee3b164"; String userId="5732f0bf39254486a3e8a67991ac4ea7"; String password= "z-_SYiu0}T7.(2SZ"; String domainName="1365701";*/
//OR USE PROPERTIES FILE String userId = Manager.getProperties("integrationservices").getProperty("cos.userId"); String password = Manager.getProperties("integrationservices").getProperty("cos.password"); String auth_url = Manager.getProperties("integrationservices").getProperty("cos.auth_url"); String project = Manager.getProperties("integrationservices").getProperty("cos.project"); String domainName = Manager.getProperties("integrationservices").getProperty("cos.domainName");
try { wfc.harnessRegister(); String action = wfc.getParm("operation");//PUT, GET, DELETE, MKDIR String local_path = wfc.getParm("local_path");//Path to read file from for uploading String messageId = wfc.getParm("messageId");//Message ID for mailbox String container_name = (String)wfc.getWFDContent("container_name");
String file_Name = wfc.getParm("file_Name"); //Name with which resource resides in Cloud object Storage String folderName = wfc.getParm("foldername");//Folder name OR structure
Identifier domainIdent = Identifier.byName(domainName); Identifier projectIdent = Identifier.byName(project);
OSClient osClient = OSFactory.builderV3().endpoint(auth_url) .credentials(userId, password) .scopeToProject(projectIdent, domainIdent) .authenticate();
log.log("ObjectStorage Client is created!! Connection established!!");
// create container - name must be unique for all S3 users if(container_name!=null){ log.log("container_name"+container_name); System.out.println("container_name"+container_name); try{ ActionResponse ac=osClient.objectStorage().containers().create(container_name); }catch(Exception e){ e.printStackTrace(); System.out.println("Container trace"+e.getMessage()); }
} // list containers List<? extends SwiftContainer> containers = osClient.objectStorage().containers().list();
for(int i=0;i<containers.size();i++){ log.log("-"+containers.get(i)); }
//Operations on files if ("put".equalsIgnoreCase(action)) { if ( wfc.getPrimaryDocument() != null ){// put file into COS docAsStream = wfc.getPrimaryDocument().getBodyInputStream(); putFileToCOS(docAsStream, file_Name, osClient, container_name); } else if (local_path != null && !"".equalsIgnoreCase(local_path)) {// put file from FileSystem to COS docAsStream = new FileInputStream( new File( local_path )); putFileToCOS(docAsStream, file_Name, osClient, container_name); } else if (messageId != null && !"".equalsIgnoreCase(messageId) && !"null".equalsIgnoreCase(messageId)) {// put file from mailbox to COS putFileFromMailboxToCOS(messageId, file_Name, osClient, container_name); }
} else if ("get".equalsIgnoreCase(action)) {// read files from COS log.log("Usage: s3client read <s3_key>"); readFileFromCOS(file_Name, osClient, container_name); wfc.putPrimaryDocument(primaryDoc);
} else if ("delete".equalsIgnoreCase(action)) {// Delete files from COS log.log("Usage: s3client delete <s3_key>"); deleteFileFromCOS(file_Name, osClient, container_name);
} else if ("mkdir".equalsIgnoreCase(action)) {// create folder into Container of COS log.log("Usage: s3client mkdir <s3_key>"); createFolderIntoContainer(osClient, container_name,folderName);
} else { log.log("Usage: s3client put/read/delete/mkdir" + " [<local_path> <s3_key>]"); }
log.log("Done!"); } catch (Exception e) { isOK = false; e.printStackTrace(); System.out.println(e); log.log(e.getMessage()); log.logException(e); wfc.setBasicStatus(1); throw new WorkFlowException(e.toString()); } finally { wfc.unregisterThread(); if (isOK) { wfc.setBasicStatusSuccess(); } else { wfc.setBasicStatusError(); } } return wfc;
} public void createFolderIntoContainer(OSClient osClient, String container_name, String folderName) {
osClient.objectStorage().containers().createPath(container_name, "Myfolder"); } public void deleteFileFromCOS(String file_Name, OSClient osClient, String container_name) { System.out.println("===========File Delete============"); osClient.objectStorage().objects().delete(container_name,file_Name); System.out.println("file deleted succesfully");
} public void readFileFromCOS(String file_Name, OSClient osClient, String container_name) throws IOException {
SwiftObject file = osClient.objectStorage().objects().get(container_name, file_Name); DLPayload downloadLPayload=file.download(); InputStream in=downloadLPayload.getInputStream();
OutputStream out;
//String filename = file_Name.substring(file_Name.lastIndexOf('/') + 1, file_Name.length()); //out = new BufferedOutputStream(new FileOutputStream(new File("C:\\NAVEEN\\newfile.txt1"))); out = new BufferedOutputStream(new FileOutputStream(new File(file_Name)));
byte[] b = new byte[1024]; int numBytes = 0; while ((numBytes = in.read(b)) > 0) { out.write(b, 0, numBytes); } in.close(); out.close(); primaryDoc = new Document(); primaryDoc.setBody(b); }
public void putFileFromMailboxToCOS(String messageId, String file_Name, OSClient osClient, String container_name) throws IOException { IRepository repos = MailboxFactory.getRepository(); String filename = null; Message message = null; Document batchDoc = null; try { log.log("CloudObjectStorageClient.putFileFromMailboxToS3():messageId:" + messageId); message = repos.getMessage(Long.parseLong(messageId));// source here is messageid log.log("CloudObjectStorageClient.putFileFromMailboxToS3(): message:" + message.toString()); String docId = message.getDocumentId(); log.log("CloudObjectStorageClient.putFileFromMailboxToS3(): docId:" + docId); filename = message.getMessageName(); log.log("CloudObjectStorageClient.putFileFromMailboxToS3():filename:" + filename); if (docId != null) { batchDoc = new Document(docId, true); } log.log("CloudObjectStorageClient.putFileFromMailboxToS3(): batchDoc:" + batchDoc); } catch (NumberFormatException nfe) { log.logException(nfe); } catch (MailboxException me) { log.logException(me); } catch (DocumentNotFoundException dnfe) { log.logException(dnfe); } catch (SQLException sqle) { log.logException(sqle); }
InputStream in = batchDoc.getInputStream(); final File aFile = File.createTempFile("tmp", "txt"); aFile.deleteOnExit(); try{ FileOutputStream out = new FileOutputStream(aFile); IOUtils.copy(in, out); } catch (IOException ioe) { log.logException(ioe); } System.out.println("===========File Upload============"); String success = osClient.objectStorage().objects().put(container_name, file_Name, Payloads.create(aFile)); if(success!=null){ System.out.println("File Uploaded succesfully");}
} public void putFileToCOS(InputStream source, String file_Name, OSClient osClient, String container_name) { System.out.println("===========File Upload============"); String success = osClient.objectStorage().objects().put(container_name, file_Name, Payloads.create(source)); if(success!=null){ System.out.println("File Uploaded succesfully");} } }
|