Other Attributes (Java)
This tutorial walks you through all the steps needed to start using Other Attributes (such as External Attributes) in Content Server using the CWS API with Java and Eclipse.
Outline
- Create a New Project
- Create CWS Client Proxies
- Write Your Code
- Run the Program
- Source Code
- References
- Notes
Create a New Project
The first thing we need is a new project in Eclipse to work with.
-
Open Eclipse.
-
Open the File menu and select New → Java Project.
-
Enter a name for the project and then select Finish.
Create CWS Client Proxies
Next, we need create the client proxies for each CWS service we want to use.
-
Open a new command prompt.
-
Use the wsimport command to generate the client proxies for each service. Enter the following commands:
wsimport -keep http://localhost:8080/cws/services/Authentication?wsdl
wsimport -keep http://localhost:8080/cws/services/DocumentManagement?wsdl
-
Use the jar command to bundle the generated class files into a .jar file. Enter the following command:
jar cvfM cws.jar com/opentext*
-
Copy the cws.jar file into your eclipse project and add it to your build path by right clicking the cws.jar file in the Package Explorer and select Build Path → Add to Build Path.
Write Your Code
Now that the CWS services are ready to use we can go ahead and start writing some code. The first thing to do is to authenticate a user. We will use basic Content Server authentication using the CWS Authentication service. You will need to change the USERNAME
and PASSWORD
to use valid credentials of a user on your system.
// The user's credentials final String USERNAME = "username"; final String PASSWORD = "password"; // Create the Authentication service client Authentication_Service authService = new Authentication_Service(); Authentication authClient = authService.getBasicHttpBindingAuthentication(); // Store the authentication token String authToken = null; // Call the AuthenticateUser() method to get an authentication token try { System.out.print("Authenticating User..."); authToken = authClient.authenticateUser(USERNAME, PASSWORD); System.out.println("SUCCESS!\n"); } catch (SOAPFaultException e) { System.out.println("FAILED!\n"); System.out.println(e.getFault().getFaultCode() + " : " + e.getMessage()); return; }
In this example we will be dealing with External Attributes such as:
- 'External Create Date': Date external data object was created
- 'External Identity': Account used to create external data object
- 'External Identity Type': Type of account used to create external data object
- 'External Modify Date': Date when external data object was last modified
- 'External Source': Source type specified by creator of external data object
// Create the DocumentManagement service client DocumentManagement_Service docManService = new DocumentManagement_Service(); DocumentManagement docManClient = docManService.getBasicHttpBindingDocumentManagement(); // Create the OTAuthentication object and set the authentication token OTAuthentication otAuth = new OTAuthentication(); otAuth.setAuthenticationToken(authToken); // We need to manually set the SOAP header to include the authentication token try { // The namespace of the OTAuthentication object final String ECM_API_NAMESPACE = "urn:api.ecm.opentext.com"; // Create a SOAP header SOAPHeader header = MessageFactory.newInstance().createMessage().getSOAPPart().getEnvelope().getHeader(); // Add the OTAuthentication SOAP header element SOAPHeaderElement otAuthElement = header.addHeaderElement(new QName(ECM_API_NAMESPACE, "OTAuthentication")); // Add the AuthenticationToken SOAP element SOAPElement authTokenElement = otAuthElement.addChildElement(new QName(ECM_API_NAMESPACE, "AuthenticationToken")); authTokenElement.addTextNode(otAuth.getAuthenticationToken()); // Set the SOAP header on the docManClient ((WSBindingProvider) docManClient).setOutboundHeaders(Headers.create(otAuthElement)); } catch (SOAPException e) { System.out.println("Failed to set authentication SOAP header!\n"); System.out.println(e.getMessage()); return; }
Next, we will ask the document management client to get us an External Attribute template. You can do this by calling the Document Management's "GetAttributeGroupTemplate" method and pass in "ExternalAtt" for the 'Type' and 'Key' parameters (see the example below). This will save us having to create the object structure ourselves, and allow us to just fill in the values for each attribute from the template.
AttributeGroup externalAttTemplate = null; // Call the GetAttributeGroupTemplate() method to get the external attribute group template try { System.out.print("Getting External Attributes Template..."); externalAttTemplate = docManClient.GetAttributeGroupTemplate("ExternalAtt", "ExternalAtt"); System.out.println("SUCCESS!\n"); } catch (SOAPFaultException e) { System.out.println("FAILED!"); System.out.println(e.getFault().getFaultCode() + " : " + e.getMessage()); return; }
Finally, we will create a document and set its external attributes. You will need to change the FILE_PATH
to point to a file on your local system.
File file = new File(FILE_PATH); // Get the file attributes byte[] contents = null; BasicFileAttributes fileAttributes = null; try { contents = Files.readAllBytes(file.toPath()); fileAttributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class); } catch (IOException e) { System.out.println("Failed to read file attributes!\n"); System.out.println(e.getMessage()); return; } // Create the attachment Attachment attachment = new Attachment(); try { attachment.setContents(contents); attachment.setCreatedDate(DatatypeFactory.newInstance().newXMLGregorianCalendar(fileAttributes.creationTime().toString())); attachment.setFileName(file.getName()); attachment.setFileSize(file.length()); attachment.setModifiedDate(DatatypeFactory.newInstance().newXMLGregorianCalendar(fileAttributes.lastModifiedTime().toString())); } catch (DatatypeConfigurationException e) { System.out.println("Failed to set file attributes!\n"); System.out.println(e.getMessage()); return; } // External Create Date DateValue externalCreateDate = (DateValue) externalAttTemplate.getValues().get(0); externalCreateDate.getValues().clear(); try { externalCreateDate.getValues().add(DatatypeFactory.newInstance().newXMLGregorianCalendar(fileAttributes.creationTime().toString())); } catch (DatatypeConfigurationException e) { System.out.println("Error creating new XMLGregorianCalendar!"); System.out.println(e.getMessage()); return; } // External Identity StringValue externalIdentity = (StringValue) externalAttTemplate.getValues().get(1); externalIdentity.getValues().clear(); externalIdentity.getValues().add("1000"); // External Identity Type StringValue externalIdentityType = (StringValue) externalAttTemplate.getValues().get(2); externalIdentityType.getValues().clear(); externalIdentityType.getValues().add("generic_userid"); // External Modify Date DateValue externalModifyDate = (DateValue) externalAttTemplate.getValues().get(3); externalModifyDate.getValues().clear(); try { externalModifyDate.getValues().add(DatatypeFactory.newInstance().newXMLGregorianCalendar(fileAttributes.lastModifiedTime().toString())); } catch (DatatypeConfigurationException e) { System.out.println("Error creating new XMLGregorianCalendar!"); System.out.println(e.getMessage()); return; } // External Source StringValue externalIdentityType = (StringValue) externalAttTemplate.getValues().get(4); externalIdentityType.getValues().clear(); externalIdentityType.getValues().add("file_system"); Metadata metadata = new Metadata(); metadata.getAttributeGroups().add(externalAttTemplate); Node theMatrix = null; // Call the createDocument() method to create the document try { System.out.print("Creating Document With External Attributes..."); theMatrix = docManClient.createDocument(PARENT_ID, "The Matrix", null, false, metadata, attachment); System.out.println("SUCCESS!\n"); System.out.println("Document ID = " + theMatrix.getID()); } catch (SOAPFaultException e) { System.out.println("FAILED!"); System.out.println(e.getFault().getFaultCode() + " : " + e.getMessage()); }
Run the Program
If everything is setup correctly you should be able to successfully run the program. You can double check that everything worked by looking at your Content Server instance in a browser and verifying the document was created successfully and it has the above external attributes (which can be found in the Function Menu > Properties page).
Source Code
References
- Eclipse - The Eclipse Foundation Open Source Community Website
- Oracle Java SE Documentation - wsimport
- Oracle Java SE Documentation - jar
- Oracle Java Tutorials - Creating a JAR File
Notes
-
How do I get Category inheritance or set no Categories when creating a node with External Attributes?
Category inheritance
Method 1: pass in a null for the Metadata parameter. The External Attributes will be set to their default values (ex. null for dates, empty strings for strings).theMatrix = docManClient.createDocument(PARENT_ID, "The Matrix", null, false, null, attachment);
Method 2: create a new AttributeGroup, set the 'Type' to 'Category' and pass it in when setting Metadata's AttributeGroups list (this 'empty' Category AttributeGroup must occur only once in the list, but it doesn't have to be the only thing in there). Use this when you're also specifying an 'ExternalAtt' Attribute Group.// Set up your External Attributes as described in the example AttributeGroup externalAttTemplate = null; // Call the GetAttributeGroupTemplate() method to get the external attribute group template try { System.out.print("Getting External Attributes Template..."); externalAttTemplate = docManClient.GetAttributeGroupTemplate("ExternalAtt", "ExternalAtt"); System.out.println("SUCCESS!\n"); } catch (SOAPFaultException e) { System.out.println("FAILED!"); System.out.println(e.getFault().getFaultCode() + " : " + e.getMessage()); return; } AttributeGroup catAttrGroup = new AttributeGroup(); catAttrGroup.setType("Category"); Metadata metadata = new Metadata(); metadata.getAttributeGroups().add(catAttrGroup); metadata.getAttributeGroups().add(externalAttTemplate); theMatrix = docManClient.createDocument(PARENT_ID, "The Matrix", null, false, metadata, attachment);
Set no Categories
Method 1: pass in an empty Metadata object for the Metadata parameterMetadata metadata = new Metadata(); theMatrix = docManClient.createDocument(PARENT_ID, "The Matrix", null, false, metadata, attachment);
Method 2: do not pass in any Category AttributeGroups. This is useful when Metadata contains an External Attribute group.Metadata metadata = new Metadata(); // externalAttrGroup is an AttributeGroup of Type 'Externalatt' metadata.getAttributeGroups().add(externalAttrGroup); theMatrix = docManClient.createDocument(PARENT_ID, "The Matrix", null, false, metadata, attachment);