OpenText | Content Web Services

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

The first thing we need is a new project in Eclipse to work with.

  1. Open Eclipse.

  2. Open the File menu and select New → Java Project.

  3. Enter a name for the project and then select Finish.

    Create New Java Project

Create CWS Client Proxies

Next, we need create the client proxies for each CWS service we want to use.

  1. Open a new command prompt.

  2. 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

    wsimport

  3. Use the jar command to bundle the generated class files into a .jar file. Enter the following command:

    jar cvfM cws.jar com/opentext*

    Create cws.jar File

  4. 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.

    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:

We will need to create the document management service client and the auth token.


// 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).

  1. Open the Run menu and select Run (Ctrl + F11).

    Result

Source Code

References

Notes