001: // CCPPRequest.java
002: // $Id: CCPPRequest.java,v 1.4 2000/08/16 21:37:34 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 2000.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.ccpp;
007:
008: import org.w3c.jigsaw.http.Reply;
009: import org.w3c.jigsaw.http.Request;
010:
011: import org.w3c.www.http.HttpExtList;
012: import org.w3c.www.http.HttpExt;
013: import org.w3c.www.http.HttpFactory;
014: import org.w3c.www.http.HttpTokenList;
015:
016: /**
017: * @version $Revision: 1.4 $
018: * @author Benoît Mahé (bmahe@w3.org)
019: */
020: public class CCPPRequest implements CCPP {
021:
022: Request request = null;
023: ProfileRef references[] = null;
024: HttpExtList httpextlist = null;
025:
026: /**
027: * Get the HTTP Request
028: * @return a Request
029: */
030: public Request getHTTPRequest() {
031: return request;
032: }
033:
034: /**
035: * Get the standard CCPP reason phrase for the given warning code.
036: * @param warning The given warning code.
037: * @return A String giving the standard reason phrase, or
038: * <strong>null</strong> if the status doesn't match any knowned error.
039: */
040: public static String getStandardWarning(int warning) {
041: int category = warning / 100;
042: int catcode = warning % 100;
043: switch (category) {
044: case 1:
045: if ((catcode >= 0) && (catcode < msg_100.length))
046: return msg_100[catcode];
047: break;
048: case 2:
049: if ((catcode >= 0) && (catcode < msg_200.length))
050: return msg_200[catcode];
051: break;
052: }
053: return UNKNOWN_WARNING_MESSAGE;
054: }
055:
056: /**
057: * Get a header value (relative to the CC/PP Extension protocol)
058: * @param request the HTTP Request
059: * @param header the header name (ie "Profile")
060: * @return a String.
061: */
062: public String getCCPPHeaderValue(String header) {
063: return request.getExtHeader(HTTP_EXT_ID, header);
064: }
065:
066: /**
067: * Get the profile diff header relative to the given profile diff number.
068: * @param request the HTTP Request
069: * @param diffnumber the diff number
070: */
071: public String getProfileDiff(int diffnumber) {
072: String diffname = PROFILE_DIFF_HEADER + "-" + diffnumber;
073: return getCCPPHeaderValue(diffname);
074: }
075:
076: /**
077: * Get the Profile references (absolute URI or Profile-diff-name)
078: * ordered by priority (the last one has the highest priority).
079: * @return a ProfileRef array (or null)
080: * @see ProfileRef
081: */
082: public ProfileRef[] getProfileReferences() {
083: if (references == null) {
084: String profile = getCCPPHeaderValue(PROFILE_HEADER);
085: if (profile != null) {
086: String profiles[] = (String[]) HttpFactory
087: .parseTokenList(profile).getValue();
088: references = new ProfileRef[profiles.length];
089: for (int i = 0; i < references.length; i++) {
090: references[i] = new ProfileRef(profiles[i]);
091: }
092: } else {
093: return null;
094: }
095: }
096: return references;
097: }
098:
099: /**
100: * Get the CC/PP Request associated to the given HTTP Request
101: * @param request the HTTP Request
102: * @return a CCPPRequest instance
103: */
104: public static CCPPRequest getCCPPRequest(Request request) {
105: if (request.hasState(CCPP_REQUEST_STATE)) {
106: return (CCPPRequest) request.getState(CCPP_REQUEST_STATE);
107: } else {
108: CCPPRequest ccpprequest = new CCPPRequest(request);
109: request.setState(CCPP_REQUEST_STATE, ccpprequest);
110: return ccpprequest;
111: }
112: }
113:
114: /**
115: * Set the Acknowledgement Headers if it's appropriate.
116: * @param reply the reply
117: * @return the aknowledged reply
118: */
119: protected Reply acknowledge(Reply reply) {
120: HttpExtList man = request.getHttpManExtDecl();
121: if ((man != null) && (man.getLength() == 1)
122: && (man.getHttpExt(HTTP_EXT_ID) != null)) {
123: reply.setEnd2EndExtensionAcknowledgmentHeader();
124: }
125:
126: HttpExtList cman = request.getHttpCManExtDecl();
127: if ((cman != null) && (cman.getLength() == 1)
128: && (cman.getHttpExt(HTTP_EXT_ID) != null)) {
129: reply.setHopByHopExtensionAcknowledgmentHeader();
130: }
131:
132: return reply;
133: }
134:
135: /**
136: * Add a CC/PP Warning to the given reply.
137: * @param reply the HTTP Reply
138: * @param warning the CC/PP Warning code
139: * @param reference the Profile reference
140: */
141: public void addWarning(Reply reply, int warning, String reference) {
142: CCPPWarning ccppwarning = (CCPPWarning) reply
143: .getState(CCPPWarning.CCPPWARNING_STATE);
144: if (ccppwarning == null) {
145: ccppwarning = new CCPPWarning();
146: reply.setState(CCPPWarning.CCPPWARNING_STATE, ccppwarning);
147: }
148: ccppwarning.addWarning(warning, reference);
149: // is the extension declared?
150: HttpExtList list = reply.getExtList(HTTP_EXT_ID);
151: if (list == null) {
152: list = new HttpExtList(httpextlist);
153: reply.setHttpExtDecl(list);
154: }
155: HttpExt ext = list.getHttpExt(HTTP_EXT_ID);
156: reply.setExtensionHeader(ext, PROFILE_WARNING_HEADER,
157: ccppwarning.toString());
158: }
159:
160: private CCPPRequest(Request request) {
161: this.request = request;
162: this.httpextlist = request.getExtList(HTTP_EXT_ID);
163: }
164: }
|