Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / jigsaw / ccpp / CCPPRequest.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigsaw/ccpp/CCPPRequest.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigsaw/ccpp/CCPPRequest.java
new file mode 100644 (file)
index 0000000..3b58790
--- /dev/null
@@ -0,0 +1,169 @@
+// CCPPRequest.java\r
+// $Id: CCPPRequest.java,v 1.1 2010/06/15 12:28:20 smhuang Exp $\r
+// (c) COPYRIGHT MIT, INRIA and Keio, 2000.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.jigsaw.ccpp;\r
+\r
+import org.w3c.jigsaw.http.Reply;\r
+import org.w3c.jigsaw.http.Request;\r
+\r
+import org.w3c.www.http.HttpExtList;\r
+import org.w3c.www.http.HttpExt;\r
+import org.w3c.www.http.HttpFactory;\r
+import org.w3c.www.http.HttpTokenList;\r
+\r
+/**\r
+ * @version $Revision: 1.1 $\r
+ * @author  Benoît Mahé (bmahe@w3.org)\r
+ */\r
+public class CCPPRequest implements CCPP {\r
+\r
+    Request     request      = null;\r
+    ProfileRef  references[] = null;\r
+    HttpExtList httpextlist  = null;\r
+\r
+    /**\r
+     * Get the HTTP Request\r
+     * @return a Request\r
+     */\r
+    public Request getHTTPRequest() {\r
+       return request;\r
+    }\r
+\r
+    /**\r
+     * Get the standard CCPP reason phrase for the given warning code.\r
+     * @param warning The given warning code.\r
+     * @return A String giving the standard reason phrase, or\r
+     * <strong>null</strong> if the status doesn't match any knowned error.\r
+     */\r
+    public static String getStandardWarning(int warning) {\r
+       int category = warning / 100;\r
+       int catcode  = warning % 100;\r
+       switch(category) \r
+           {\r
+           case 1:\r
+               if ((catcode >= 0) && (catcode < msg_100.length))\r
+                   return msg_100[catcode];\r
+               break;\r
+           case 2:\r
+               if ((catcode >= 0) && (catcode < msg_200.length))\r
+                   return msg_200[catcode];\r
+               break;\r
+           }\r
+       return UNKNOWN_WARNING_MESSAGE;\r
+    }\r
+\r
+    /**\r
+     * Get a header value (relative to the CC/PP Extension protocol)\r
+     * @param request the HTTP Request\r
+     * @param header the header name (ie "Profile")\r
+     * @return a String.\r
+     */\r
+    public String getCCPPHeaderValue(String header) {\r
+       return request.getExtHeader(HTTP_EXT_ID, header);\r
+    }\r
+\r
+    /**\r
+     * Get the profile diff header relative to the given profile diff number.\r
+     * @param request the HTTP Request\r
+     * @param diffnumber the diff number\r
+     */\r
+    public String getProfileDiff(int diffnumber) \r
+    {\r
+       String diffname = PROFILE_DIFF_HEADER+"-"+diffnumber;\r
+       return getCCPPHeaderValue(diffname);\r
+    }\r
+\r
+    /**\r
+     * Get the Profile references (absolute URI or Profile-diff-name)\r
+     * ordered by priority (the last one has the highest priority).\r
+     * @return a ProfileRef array (or null)\r
+     * @see ProfileRef\r
+     */\r
+    public ProfileRef[] getProfileReferences() {\r
+       if (references == null) {\r
+           String profile = getCCPPHeaderValue(PROFILE_HEADER);\r
+           if (profile != null) {\r
+               String profiles[] = \r
+                   (String[]) HttpFactory.parseTokenList(profile).getValue();\r
+               references = new ProfileRef[profiles.length];\r
+               for (int i = 0 ; i < references.length ; i++) {\r
+                   references[i] = new ProfileRef(profiles[i]);\r
+               }\r
+           } else {\r
+               return null;\r
+           }\r
+       }\r
+       return references;\r
+    }\r
+\r
+    /**\r
+     * Get the CC/PP Request associated to the given HTTP Request\r
+     * @param request the HTTP Request\r
+     * @return a CCPPRequest instance\r
+     */\r
+    public static CCPPRequest getCCPPRequest(Request request) {\r
+       if (request.hasState(CCPP_REQUEST_STATE)) {\r
+           return (CCPPRequest) request.getState(CCPP_REQUEST_STATE);\r
+       } else {\r
+           CCPPRequest ccpprequest = new CCPPRequest(request);\r
+           request.setState(CCPP_REQUEST_STATE, ccpprequest);\r
+           return ccpprequest;\r
+       }\r
+    }\r
+\r
+    /**\r
+     * Set the Acknowledgement Headers if it's appropriate.\r
+     * @param reply the reply\r
+     * @return the aknowledged reply\r
+     */\r
+    protected Reply acknowledge(Reply reply) {\r
+       HttpExtList man = request.getHttpManExtDecl();\r
+       if ((man != null) && \r
+           (man.getLength() == 1) &&\r
+           (man.getHttpExt(HTTP_EXT_ID) != null)) {\r
+           reply.setEnd2EndExtensionAcknowledgmentHeader();\r
+       }\r
+\r
+       HttpExtList cman = request.getHttpCManExtDecl();\r
+       if ((cman != null) && \r
+           (cman.getLength() == 1) &&\r
+           (cman.getHttpExt(HTTP_EXT_ID) != null)) {\r
+           reply.setHopByHopExtensionAcknowledgmentHeader();\r
+       }\r
+\r
+       return reply;\r
+    }\r
+\r
+    /**\r
+     * Add a CC/PP Warning to the given reply.\r
+     * @param reply the HTTP Reply\r
+     * @param warning the CC/PP Warning code\r
+     * @param reference the Profile reference\r
+     */\r
+    public void addWarning(Reply reply, int warning, String reference) {\r
+       CCPPWarning ccppwarning = (CCPPWarning)\r
+           reply.getState(CCPPWarning.CCPPWARNING_STATE);\r
+       if (ccppwarning == null) {\r
+           ccppwarning = new CCPPWarning();\r
+           reply.setState(CCPPWarning.CCPPWARNING_STATE, ccppwarning);\r
+       }\r
+       ccppwarning.addWarning(warning, reference);\r
+       // is the extension declared?\r
+       HttpExtList list = reply.getExtList(HTTP_EXT_ID);\r
+       if (list == null) {\r
+           list = new  HttpExtList(httpextlist);\r
+           reply.setHttpExtDecl(list);\r
+       }\r
+       HttpExt ext = list.getHttpExt(HTTP_EXT_ID);\r
+       reply.setExtensionHeader(ext, \r
+                                PROFILE_WARNING_HEADER, \r
+                                ccppwarning.toString());\r
+    }\r
+\r
+    private CCPPRequest(Request request) {\r
+       this.request     = request;\r
+       this.httpextlist = request.getExtList(HTTP_EXT_ID);\r
+    }\r
+}\r