Wednesday, February 27, 2013

consume a basic auth web service in C# and manipulate the output


This is a C# client program for a webMethods web service with basic authentication setup. This service will also loop over the output from the WS call and perform a basic extraction of the data.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace reportFraudConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            reportfrauddataref.reportfraudVS ws = new reportfrauddataref.reportfraudVS();

            reportfrauddataref.getCaseAttachmentFilesInputDoc wsinput = new reportfrauddataref.getCaseAttachmentFilesInputDoc();
            wsinput.caseNr = "5555";

            //auth with CredentialCache class
            System.Net.CredentialCache myCred = new System.Net.CredentialCache();
            NetworkCredential networkCred = new NetworkCredential("Administrator", "wm123");
            myCred.Add(new Uri(ws.Url),"Basic",networkCred);
            ws.Credentials = myCred;

            reportfrauddataref.attachmentsData[] resultArray = ws.getCaseAttachmentFiles(wsinput);

            List fileNames=new List();
            List fileContents=new List();

            for (int i = 0; i < resultArray.Length;i++ )
            {
                // Manipulate the output from the web service by looping over the results from the ws call
                fileNames.Add(resultArray[i].fileName);
                fileContents.Add(resultArray[i].fileContent);
            }

            Console.WriteLine(fileNames.ToArray().ToString());
            Console.ReadLine();
        }
    }
}

Wednesday, February 6, 2013

List files and directories recursively

This java service will list all the files and the files in the sub-directories with full path names.


IDataCursor pipelineCursor = pipeline.getCursor();

String    dirPath = IDataUtil.getString( pipelineCursor, "dirPath" );

pipelineCursor.destroy();

         

List<String> output=new ArrayList<String>();

         

List<String> tempPlaceHolder=new ArrayList<String>();

output=extract(dirPath,tempPlaceHolder);

  

IDataUtil.put( pipelineCursor, "dirList", output.toArray(new String[output.size()]));

pipelineCursor.destroy();
Shared Code:
public static List<String> extract(String directoryName, List<String> outFiles){

    
File directory=new File(directoryName);
File[] fileList=directory.listFiles();

String outFileName;
for(int i=0;i<fileList.length;i++){
 if(fileList[i].isFile()){
 outFileName=fileList[i].toString();
 outFiles.add(outFileName);
        }
 else{

      outFileName=fileList[i].toString();
      outFiles.add(outFileName);
      extract(outFileName,outFiles);               

     }           
    }

return outFiles;
}

Tuesday, February 5, 2013

Java service to create a Tar file using Apache Commons Compress library.

There is often a need to create a Tar file by archiving logs files or other files. You could create a tar file using Native Java libraries as well as third party libraries. The advantage of using third party libraries is that it  provides out of the box methods to write the files. The disadvantage would be to go through the license agreements on the jars and make sure you are not violating any of their agreements.


IDataCursor pipelineCursor = pipeline.getCursor();
String directoryName = IDataUtil.getString( pipelineCursor, "directoryName" );
pipelineCursor.destroy();
  
File dirName=new File(directoryName);
File[] fileNames=dirName.listFiles();  
OutputStream tarfileOutstream;  
try {
      tarfileOutstream = new FileOutputStream(new File("E:/aarremreddy/Tasks/Compress/test.tar"));
      ArchiveOutputStream aos = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.TAR, tarfileOutstream);
     
      for(int i=0;i<fileNames.length;i++){
 File tarInputfile= fileNames[i];
 TarArchiveEntry archiveEntry = new TarArchiveEntry(tarInputfile);
 archiveEntry.setSize(tarInputfile.length());
 aos.putArchiveEntry(archiveEntry);
 IOUtils.copy(new FileInputStream(tarInputfile),aos);
 aos.closeArchiveEntry();
      }

    aos.finish(); 
    tarfileOutstream.close();
    IDataUtil.put(pipelineCursor, "result", "Success");
} 
catch (FileNotFoundException e) {
 IDataUtil.put(pipelineCursor, "result", e.toString());
} catch (ArchiveException e) {
 IDataUtil.put(pipelineCursor, "result", e.toString());
} catch (IOException e) {
 IDataUtil.put(pipelineCursor, "result", e.toString());
}
Imports:
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.utils.IOUtils;
Jars:
commons-compress-1.4.1.jar;
commons-io-2.4.jar;