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());

}

使用代码http://developer.arx.com/quick-start/sapi-web-services/#t-helloworld。我编写了与 java 1.6 兼容的代码,我收到错误“AccessControlException:访问被拒绝”,我是遗漏了什么?

4

2 回答 2

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.");
}
于 2014-06-19T10:11:55.547 回答
0

请注意,您的 API 用户名是您的电子邮件,而不是您用于开发人员门户的用户名。

服务点 url 显示在api 概述的入门选项卡上

试用/开发者系统的服务点 url

于 2014-06-20T11:41:03.820 回答