This is an archive of an inactive wiki and cannot be modified.

UseCCPPOrUAProf

This technique gives a possible implementation for the CategoryBpContext using JSR-188. The idea is to capture client's profile information (CC/PP or, more likely, UAProf) so the context of the client is known in order to server appropriate content. The idea of the example shown below is a servlet to return to the client a list of the attributes and values in profile information as declared by the client via HTTP GET request's headers. Most of desktop user agents do not provide CC/PP and/or UAProf information so the example should be tested at client side by a mobile device or an emulator.

Pre-requisites:

Implementation Steps:

Example:

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Iterator;
import java.util.Set;
 
import javax.ccpp.Attribute;
import javax.ccpp.Profile;
import javax.ccpp.ProfileFactory;
import javax.ccpp.ValidationMode;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.sun.ccpp.DescriptionManager;
import com.sun.ccpp.ProfileFactoryImpl;
 
 
public class JavaCCPPExample extends javax.servlet.http.HttpServlet implements
    javax.servlet.Servlet
{
 
  public JavaCCPPExample()
  {
    super();
  }
 
  // Servlet's initialization method where the schema of the vocabulary is set 
  // and all vocabularies to be processed by are configured. This configuration 
  // process is very important for the servlet to properly work
  public void init(ServletConfig config)
  {
    ServletContext context = null;
    String VocabularyPath = "\\Vocabularies\\";
    DescriptionManager dm = null;
    URL vocab = null;
    URL schema = null;  
 
    // Get the context of the servlet to get resources later
    context = config.getServletContext();
 
    try
    {
      // Set the schema followed by vocabularies
      schema = context.getResource(VocabularyPath + "vocabulary.xsd");
      DescriptionManager.setSchema(schema.openStream());
      
      // Get an instance of the Description Manager in order to add several
      // vocabularies
      dm = DescriptionManager.getInstance();
      
      // Add a set of vocabularies which will allow proper processing  
      // of CC/PP and UAProf profiles
      vocab = context.getResource(VocabularyPath + "universalvocab.xml");
      dm.addVocabulary(vocab.openStream());
      vocab = context.getResource(VocabularyPath + "example.xml");
      dm.addVocabulary(vocab.openStream());
      vocab = context.getResource(VocabularyPath + "ccppschema-19991014.xml");
      dm.addVocabulary(vocab.openStream());
      vocab = context.getResource(VocabularyPath + "ccppschema-20000405.xml");
      dm.addVocabulary(vocab.openStream());
      vocab = context.getResource(VocabularyPath + "ccppschema-20000405a.xml");
      dm.addVocabulary(vocab.openStream());
      vocab = context.getResource(VocabularyPath + "ccppschema-20000405b.xml");
      dm.addVocabulary(vocab.openStream());
      vocab = context.getResource(VocabularyPath + "ccppschema-20010111.xml");
      dm.addVocabulary(vocab.openStream());
      vocab = context.getResource(VocabularyPath + "ccppschema-20010330.xml");
      dm.addVocabulary(vocab.openStream());
      vocab = context.getResource(VocabularyPath + "ccppschema-20010330a.xml");
      dm.addVocabulary(vocab.openStream());
      vocab = context.getResource(VocabularyPath + "ccppschema-20010330b.xml");
      dm.addVocabulary(vocab.openStream());
      vocab = context.getResource(VocabularyPath + "ccppschema-20010430.xml");
      dm.addVocabulary(vocab.openStream());
      vocab = context.getResource(VocabularyPath + "ccppschema-20010430a.xml");
      dm.addVocabulary(vocab.openStream());
      vocab = context.getResource(VocabularyPath + "ccppschema-20010430b.xml");
      dm.addVocabulary(vocab.openStream());
      vocab = context.getResource(VocabularyPath + "ccppschema-20020710.xml");
      dm.addVocabulary(vocab.openStream());
      vocab = context.getResource(VocabularyPath + "ccppschema-20020710a.xml");
      dm.addVocabulary(vocab.openStream());
      vocab = context.getResource(VocabularyPath + "ccppschema-20020710b.xml");
      dm.addVocabulary(vocab.openStream());
      vocab = context.getResource(VocabularyPath + "ccppschema-20021212.xml");
      dm.addVocabulary(vocab.openStream());
      vocab = context.getResource(VocabularyPath + "ccppschema-20030226.xml");
      dm.addVocabulary(vocab.openStream());
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
 
  }
 
  // Method to serve GET requests made from the client and get a list of client's  
  // capabilities (as long as it publishes its CC/PP and/or UAProf profile)
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException
  {
    // Object to access profile factory (see JSR-188 doc)
    ProfileFactory pf = null;
    // Object created for profile access
    Profile profile = null;
    
    
    // Set MIME type for content returned by the servlet
    response.setContentType("text/html");
 
    // Get a "PrintWriter" object that will be used to create (X)HTML code returned
    // to the client
    PrintWriter out = response.getWriter();
 
    out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    out.print("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" ");
    out.println("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
    out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
    out.println("<head>");
 
    out.println("<title>Servlet to process CC/PP over HTTP</title>");
    out.println("</head>");
    out.println("<body>");
 
    // Get an instance of a profile factory
    pf = ProfileFactoryImpl.getInstance();
    
    // Try to discover the profile declared by the client in HTTP request.
    // Validation mode is deactivated in order to be able to process most of the profiles.
    // Setting weak or strong validation modes might lead to some profiles not being
    // processed or not all the atributes being listed
    profile = pf.newProfile(request,ValidationMode.VALIDATIONMODE_NONE);
    //profile = pf.newProfile(request, ValidationMode.VALIDATIONMODE_WEAK);
    //profile = pf.newProfile(request, ValidationMode.VALIDATIONMODE_STRONG);
    
    // If profile is null then client has not declared any profile or profile is wrong
    if (profile == null)
    {
      out.println("<p>Client has not declared CC/PP and/or UAProf information</p>");
    }
    // Otherwise profile exists and is OK
    else
    {
      // Get the set of attributes declared
      Set allAttributes = profile.getAttributes();
      Iterator attrib = allAttributes.iterator();
 
      // Iterate on the set of attributes in order to get access to them attributes
      // as a list of pairs "Attribute: Value"
      while (attrib.hasNext())
      {
        Attribute attribute = (Attribute) attrib.next();
        Object value = attribute.getValue();
        out.println("<p>" + attribute.getName() + " = " + value + "</p>");
      }
    }
 
    out.println("</body>");
    out.println("</html>");
  }
 
}


Back to BestPracticesList


CategoryJava CategoryXhtml CategoryContentAdaptation

Contributions to this wiki are governed by the W3C policies for Contribution to W3C' wiki on Mobile Web.