2014-06-19 32 views
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  = "[email protected]";  // 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()); 

} 

使用代碼http://developer.arx.com/quick-start/sapi-web-services/#t-helloworld。我已經編寫了與java 1.6兼容的代碼,我收到錯誤「AccessControlException:Access denied」,我是否缺少某些東西?如何在IBM Lotus Notes中運行CoSign簽名SOAP API?

+0

所示請記住給予好評的所有有用的答案。並「檢查」最能解決您問題的答案。 –

回答

0

請注意,您的API用戶名是您的電子郵件地址,而不是您用於開發人員門戶網站的用戶名。

服務點的URL上getting started tab of the api overview

Service point url for the trial/developer system

+0

非常感謝您的幫助,代碼現在工作正常。我想對現有的代碼做一些改變,你可以看看我的新問題http://stackoverflow.com/questions/24484147/how-to-change-the-coordinates-of-signature-in-cosign-簽名的SOAP的API和-PLAC – mushir2007

1

我在代碼中發現的唯一問題是讀/寫文件字節的方式。除此之外,您的代碼對我完全運行。

這些都是我所進行的更改:

將文件轉換爲字節(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(); 
} 

附加字節到文件(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."); 
} 
+0

嗨,我已經嘗試了相同的,但它仍然拋出了同樣的錯誤「AccessControlException:訪問被拒絕」。我正在使用30天免費試用帳號,這是問題嗎?我再一次提到URL「http://arx.com/SAPIWS/DSS/1.0/」重定向到「arx.com」,這可以嗎?整體是否有任何配置更改或設置,我失蹤? – mushir2007

+0

你提到的url只是一個XML命名空間,這很好。檢查堆棧跟蹤以查看哪個方法拋出此異常。也許是因爲你沒有權限訪問部署目錄之外的文件?檢查此 - http://stackoverflow.com/questions/10454037/ –