Source Code Cross Referenced for Decoder.java in  » Web-Services » restlet-1.0.8 » com » noelios » restlet » application » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Services » restlet 1.0.8 » com.noelios.restlet.application 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.