001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: *
018: * Portions Copyright 2006 Lars Heuer (heuer[at]semagia.com)
019: */
020:
021: package com.noelios.restlet.application;
022:
023: import java.util.Iterator;
024:
025: import org.restlet.Context;
026: import org.restlet.Filter;
027: import org.restlet.data.Encoding;
028: import org.restlet.data.Request;
029: import org.restlet.data.Response;
030: import org.restlet.resource.Representation;
031:
032: /**
033: * Filter decompressing entities.
034: *
035: * @author Jerome Louvel (contact@noelios.com)
036: */
037: public class Decoder extends Filter {
038: /**
039: * Indicates if the request entity should be decoded.
040: */
041: private boolean decodeRequest;
042:
043: /**
044: * Indicates if the response entity should be decoded.
045: */
046: private boolean decodeResponse;
047:
048: /**
049: * Constructor to only decode request entities before handling.
050: *
051: * @param context
052: * The context.
053: */
054: public Decoder(Context context) {
055: this (context, true, false);
056: }
057:
058: /**
059: * Constructor.
060: *
061: * @param context
062: * The context.
063: * @param decodeRequest
064: * Indicates if the request entity should be decoded.
065: * @param decodeResponse
066: * Indicates if the response entity should be decoded.
067: */
068: public Decoder(Context context, boolean decodeRequest,
069: boolean decodeResponse) {
070: super (context);
071: this .decodeRequest = decodeRequest;
072: this .decodeResponse = decodeResponse;
073: }
074:
075: /**
076: * Allows filtering before its handling by the target Restlet. Does nothing
077: * by default.
078: *
079: * @param request
080: * The request to filter.
081: * @param response
082: * The response to filter.
083: */
084: public void beforeHandle(Request request, Response response) {
085: // Check if decoding of the request entity is needed
086: if (isDecodeRequest() && canDecode(request.getEntity())) {
087: request.setEntity(decode(request.getEntity()));
088: }
089: }
090:
091: /**
092: * Allows filtering after its handling by the target Restlet. Does nothing
093: * by default.
094: *
095: * @param request
096: * The request to filter.
097: * @param response
098: * The response to filter.
099: */
100: public void afterHandle(Request request, Response response) {
101: // Check if decoding of the response entity is needed
102: if (isDecodeResponse() && canDecode(response.getEntity())) {
103: response.setEntity(decode(response.getEntity()));
104: }
105: }
106:
107: /**
108: * Indicates if a representation can be decoded.
109: *
110: * @param representation
111: * The representation to test.
112: * @return True if the call can be decoded.
113: */
114: public boolean canDecode(Representation representation) {
115: // Test the existence of the representation and that at least an
116: // encoding applies.
117: boolean result = (representation != null)
118: && (!representation.getEncodings().isEmpty());
119:
120: if (result) {
121: boolean found = false;
122: for (Iterator<Encoding> iter = representation
123: .getEncodings().iterator(); !found
124: && iter.hasNext();) {
125: found = (!iter.next().equals(Encoding.IDENTITY));
126: }
127: result = found;
128: }
129: return result;
130: }
131:
132: /**
133: * Decodes a given representation if its encodings are supported by NRE.
134: *
135: * @param representation
136: * The representation to encode.
137: * @return The decoded representation or the original one if the encoding
138: * isn't supported by NRE.
139: */
140: public Representation decode(Representation representation) {
141: Representation result = representation;
142:
143: // Check if all encodings of the representation are supported in order
144: // to avoid the creation of a useless decodeRepresentation object.
145: // False if an encoding is not supported
146: boolean supported = true;
147: // True if all representation's encodings are IDENTITY
148: boolean identityEncodings = true;
149: for (Iterator<Encoding> iter = representation.getEncodings()
150: .iterator(); supported && iter.hasNext();) {
151: Encoding encoding = iter.next();
152: supported = DecodeRepresentation.getSupportedEncodings()
153: .contains(encoding);
154: identityEncodings &= encoding.equals(Encoding.IDENTITY);
155: }
156:
157: if (supported && !identityEncodings) {
158: result = new DecodeRepresentation(representation);
159: }
160:
161: return result;
162: }
163:
164: /**
165: * Indicates if the request entity should be decoded.
166: *
167: * @return True if the request entity should be decoded.
168: */
169: public boolean isDecodeRequest() {
170: return this .decodeRequest;
171: }
172:
173: /**
174: * Indicates if the request entity should be decoded.
175: *
176: * @param decodeRequest
177: * True if the request entity should be decoded.
178: */
179: public void setDecodeRequest(boolean decodeRequest) {
180: this .decodeRequest = decodeRequest;
181: }
182:
183: /**
184: * Indicates if the response entity should be decoded.
185: *
186: * @return True if the response entity should be decoded.
187: */
188: public boolean isDecodeResponse() {
189: return this .decodeResponse;
190: }
191:
192: /**
193: * Indicates if the response entity should be decoded.
194: *
195: * @param decodeResponse
196: * True if the response entity should be decoded.
197: */
198: public void setDecodeResponse(boolean decodeResponse) {
199: this.decodeResponse = decodeResponse;
200: }
201:
202: }
|