0
Session session = getSession();
AgentContext agentContext = session.getAgentContext();

// Custom Values
String filePath      = "C://20/ToSign.pdf";  // Pdf File to sign
String fileMimeType  = "application/pdf";   // File MIME type
String username      = "xyz@gmail.com";       // CoSign account username,Not exactly these credentials, I have entered my CoSign username and password here.
String password      = "password";          // CoSign account password
String domain        = "";                  // CoSign account domain
int sigPageNum       = 1;                   // Create signature on the first page
int sigX             = 145;                 // Signature field X location
int sigY             = 125;                 // Signature field Y location
int sigWidth         = 160;                 // Signature field width
int sigHeight        = 45;                  // Signature field height
String timeFormat    = "hh:mm:ss";          // The display format of the time
String dateFormat    = "dd/MM/yyyy";        // The display format of the date
long appearanceMask  = 11; 
String signatureType = "http://arx.com/SAPIWS/DSS/1.0/signature-field-create-sign"; // The actual operation of the Sign Request function
String wsdlUrl       = "https://prime.cosigntrial.com:8080/sapiws/dss.asmx?WSDL";   // URL to the WSDL file

// Read file contents

BufferedReader br = new BufferedReader(new FileReader(new File(filePath)));
String line;
String fileBufferContent="";
while ((line=br.readLine())!=null){
      fileBufferContent=fileBufferContent+line ;

}

byte[] fileBuffer = fileBufferContent.getBytes();

// Set file contents + MIME type (the SOAP library automatically base64 encodes the data)
DocumentType document = new DocumentType();
Base64Data base64Data = new Base64Data();
base64Data.setValue(fileBuffer);
base64Data.setMimeType(fileMimeType);
document.setBase64Data(base64Data);

// Set user credentials. In case of Active Directory, the domain name should be defined in the NameQualifier attribute
ClaimedIdentity claimedIdentity = new ClaimedIdentity();
NameIdentifierType nameIdentifier = new NameIdentifierType();
nameIdentifier.setValue(username);
nameIdentifier.setNameQualifier(domain);
CoSignAuthDataType coSignAuthData = new CoSignAuthDataType();
coSignAuthData.setLogonPassword(password);
claimedIdentity.setName(nameIdentifier);
claimedIdentity.setSupportingInfo(coSignAuthData);

System.out.println("credentials set");
// Define signature field settings
SAPISigFieldSettingsType sigFieldSettings = new SAPISigFieldSettingsType();
sigFieldSettings.setInvisible(false);
sigFieldSettings.setX(sigX);
sigFieldSettings.setY(sigY);
sigFieldSettings.setWidth(sigWidth);
sigFieldSettings.setHeight(sigHeight);
sigFieldSettings.setPage(sigPageNum);
sigFieldSettings.setAppearanceMask(appearanceMask);
TimeDateFormatType timeDateFormat = new TimeDateFormatType();
timeDateFormat.setTimeFormat(timeFormat);
timeDateFormat.setDateFormat(dateFormat);
timeDateFormat.setExtTimeFormat(ExtendedTimeFormatEnum.GMT);
sigFieldSettings.setTimeFormat(timeDateFormat);
// Build complete request object
SignRequest signRequest = new SignRequest();
RequestBaseType.InputDocuments inputDocuments = new RequestBaseType.InputDocuments();
inputDocuments.getDocumentOrTransformedDataOrDocumentHash().add(document);
RequestBaseType.OptionalInputs optionalInputs = new RequestBaseType.OptionalInputs();
optionalInputs.setSignatureType(signatureType);
optionalInputs.setClaimedIdentity(claimedIdentity);
optionalInputs.setSAPISigFieldSettings(sigFieldSettings);
optionalInputs.setReturnPDFTailOnly(true);
signRequest.setOptionalInputs(optionalInputs);
signRequest.setInputDocuments(inputDocuments);
// Initiate service client //
DSS client = new DSS(new URL(wsdlUrl), new QName("http://arx.com/SAPIWS/DSS/1.0/", "DSS"));

// Send the request
DssSignResult response = client.getDSSSoap().dssSign(signRequest);

// Check response output
if ("urn:oasis:names:tc:dss:1.0:resultmajor:Success".equals(response.getResult().getResultMajor())) {
    // On success- append signature object to the source PDF document (the SOAP library automatically decodes the base64 encoded output)
    byte[] signatureObjectBuffer = response.getSignatureObject().getBase64Signature().getValue();
    //Files.write(Paths.get(filePath), signatureObjectBuffer, StandardOpenOption.APPEND);

    BufferedWriter bw = new BufferedWriter(new FileWriter( new File(filePath)));

    String signatureObjectBufferString = signatureObjectBuffer.toString();
    bw.write(signatureObjectBufferString);
    bw.close();
}
else
{ 
    // On failure- raise exception with the result error message
    throw new Exception(response.getResult().getResultMessage().getValue());

}

using the code http://developer.arx.com/quick-start/sapi-web-services/#t-helloworld .I have written code compatible to java 1.6, I am getting errors "AccessControlException: Access denied", Am I missing something?

Almog G.
  • 817
  • 7
  • 11
mushir2007
  • 25
  • 5
  • Please remember to upvote all useful answers. And "check" the answer that best solved your question. – Larry K Jun 30 '14 at 10:09

2 Answers2

1

The only problem I found in your code is the way that you read/write the file bytes. Apart from this, your code runs perfectly to me.

These are the changes that I've made:

Converting File To Bytes (Java 1.6)

File file = new File(filePath);
byte[] fileBuffer = new byte[(int) file.length()];
try {
    FileInputStream fileInputStream = new FileInputStream(file);
    fileInputStream.read(fileBuffer);
    fileInputStream.close();
} catch (FileNotFoundException e) {
    System.out.println("File Not Found.");
    e.printStackTrace();
}
catch (IOException e1) {
    System.out.println("Error Reading The File.");
    e1.printStackTrace();
}

Appending Bytes to File (Java 1.6)

try {
    FileOutputStream fileOutputStream = new FileOutputStream(filePath, true);
    fileOutputStream.write(signatureObjectBuffer);
    fileOutputStream.close();
}
    catch(FileNotFoundException ex)   {
    System.out.println("File Not Found.");
}
    catch(IOException ioe)  {
    System.out.println("Error Reading The File.");
}
Almog G.
  • 817
  • 7
  • 11
  • Hi, I have tried the same but still it is throwing me the same error "AccessControlException: Access denied". I am using free 30 days trail account, Is this the problem? And once more I am mentioning the url "http://arx.com/SAPIWS/DSS/1.0/" redirects to "arx.com", Is this ok? and overall Is there any configuration change or setting that I am missing? – mushir2007 Jun 20 '14 at 05:34
  • The url that you mentioned is just an XML namespace, this is fine. Check the stack trace to see which method threw this exception. Maybe it's because you don't have permission to access the file outside its deployment directory? check this - http://stackoverflow.com/questions/10454037/ – Almog G. Jun 20 '14 at 14:06
0

Note that your username for the APIs is your email, not the username you use for the Developers Portal.

The service point url is shown on the getting started tab of the api overview

Service point url for the trial/developer system

Larry K
  • 47,808
  • 15
  • 87
  • 140
  • A big Thanks for the help, the code is working fine now. I would like to make some change in existing code, Can you have a look into my new question http://stackoverflow.com/questions/24484147/how-to-change-the-coordinates-of-signature-in-cosign-signature-soap-api-and-plac – mushir2007 Jun 30 '14 at 06:35