Source Code Cross Referenced for ProtocolUtil.java in  » RSS-RDF » sesame » org » openrdf » http » server » 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 » RSS RDF » sesame » org.openrdf.http.server 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
003:         *
004:         * Licensed under the Aduna BSD-style license.
005:         */
006:        package org.openrdf.http.server;
007:
008:        import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
009:        import static javax.servlet.http.HttpServletResponse.SC_NOT_ACCEPTABLE;
010:
011:        import java.util.ArrayList;
012:        import java.util.Collection;
013:        import java.util.Enumeration;
014:
015:        import javax.servlet.http.HttpServletRequest;
016:        import javax.servlet.http.HttpServletResponse;
017:
018:        import org.slf4j.Logger;
019:        import org.slf4j.LoggerFactory;
020:
021:        import info.aduna.lang.FileFormat;
022:        import info.aduna.lang.service.FileFormatServiceRegistry;
023:        import info.aduna.webapp.util.HttpServerUtil;
024:
025:        import org.openrdf.http.protocol.Protocol;
026:        import org.openrdf.http.protocol.error.ErrorInfo;
027:        import org.openrdf.http.protocol.error.ErrorType;
028:        import org.openrdf.model.Resource;
029:        import org.openrdf.model.URI;
030:        import org.openrdf.model.Value;
031:        import org.openrdf.model.ValueFactory;
032:
033:        /**
034:         * Utilities to help with the transition between HTTP requests/responses and
035:         * values expected by the protocol.
036:         * 
037:         * @author Herko ter Horst
038:         * @author Arjohn Kampman
039:         */
040:        public class ProtocolUtil {
041:
042:            public static Value parseValueParam(HttpServletRequest request,
043:                    String paramName, ValueFactory vf)
044:                    throws ClientHTTPException {
045:                String paramValue = request.getParameter(paramName);
046:                try {
047:                    return Protocol.decodeValue(paramValue, vf);
048:                } catch (IllegalArgumentException e) {
049:                    throw new ClientHTTPException(SC_BAD_REQUEST,
050:                            "Invalid value for parameter '" + paramName + "': "
051:                                    + paramValue);
052:                }
053:            }
054:
055:            public static Resource parseResourceParam(
056:                    HttpServletRequest request, String paramName,
057:                    ValueFactory vf) throws ClientHTTPException {
058:                String paramValue = request.getParameter(paramName);
059:                try {
060:                    return Protocol.decodeResource(paramValue, vf);
061:                } catch (IllegalArgumentException e) {
062:                    throw new ClientHTTPException(SC_BAD_REQUEST,
063:                            "Invalid value for parameter '" + paramName + "': "
064:                                    + paramValue);
065:                }
066:            }
067:
068:            public static URI parseURIParam(HttpServletRequest request,
069:                    String paramName, ValueFactory vf)
070:                    throws ClientHTTPException {
071:                String paramValue = request.getParameter(paramName);
072:                try {
073:                    return Protocol.decodeURI(paramValue, vf);
074:                } catch (IllegalArgumentException e) {
075:                    throw new ClientHTTPException(SC_BAD_REQUEST,
076:                            "Invalid value for parameter '" + paramName + "': "
077:                                    + paramValue);
078:                }
079:            }
080:
081:            public static Resource[] parseContextParam(
082:                    HttpServletRequest request, String paramName,
083:                    ValueFactory vf) throws ClientHTTPException {
084:                String[] paramValues = request.getParameterValues(paramName);
085:                try {
086:                    return Protocol.decodeContexts(paramValues, vf);
087:                } catch (IllegalArgumentException e) {
088:                    throw new ClientHTTPException(SC_BAD_REQUEST,
089:                            "Invalid value for parameter '" + paramName + "': "
090:                                    + e.getMessage());
091:                }
092:            }
093:
094:            public static boolean parseBooleanParam(HttpServletRequest request,
095:                    String paramName, boolean defaultValue) {
096:                String paramValue = request.getParameter(paramName);
097:                if (paramValue == null) {
098:                    return defaultValue;
099:                } else {
100:                    return Boolean.parseBoolean(paramValue);
101:                }
102:            }
103:
104:            /**
105:             * Logs all request parameters of the supplied request.
106:             */
107:            public static void logRequestParameters(HttpServletRequest request) {
108:                Logger logger = LoggerFactory.getLogger(ProtocolUtil.class);
109:                if (logger.isDebugEnabled()) {
110:                    @SuppressWarnings("unchecked")
111:                    Enumeration<String> paramNames = request
112:                            .getParameterNames();
113:                    while (paramNames.hasMoreElements()) {
114:                        String name = paramNames.nextElement();
115:                        for (String value : request.getParameterValues(name)) {
116:                            logger.debug("{}=\"{}\"", name, value);
117:                        }
118:                    }
119:                }
120:            }
121:
122:            public static <FF extends FileFormat, S> S getAcceptableService(
123:                    HttpServletRequest request, HttpServletResponse response,
124:                    FileFormatServiceRegistry<FF, S> serviceRegistry)
125:                    throws ClientHTTPException {
126:                // Accept-parameter takes precedence over request headers
127:                String mimeType = request
128:                        .getParameter(Protocol.ACCEPT_PARAM_NAME);
129:                boolean hasAcceptParam = mimeType != null;
130:
131:                if (mimeType == null) {
132:                    // Find an acceptable MIME type based on the request headers
133:                    logAcceptableFormats(request);
134:
135:                    Collection<String> mimeTypes = new ArrayList<String>(16);
136:                    for (FileFormat format : serviceRegistry.getKeys()) {
137:                        mimeTypes.addAll(format.getMIMETypes());
138:                    }
139:
140:                    mimeType = HttpServerUtil.selectPreferredMIMEType(mimeTypes
141:                            .iterator(), request);
142:
143:                    response.setHeader("Vary",
144:                            HttpServerUtil.ACCEPT_HEADER_NAME);
145:                }
146:
147:                if (mimeType != null) {
148:                    FF format = serviceRegistry
149:                            .getFileFormatForMIMEType(mimeType);
150:
151:                    if (format != null) {
152:                        return serviceRegistry.get(format);
153:                    }
154:                }
155:
156:                if (hasAcceptParam) {
157:                    ErrorInfo errInfo = new ErrorInfo(
158:                            ErrorType.UNSUPPORTED_FILE_FORMAT, mimeType);
159:                    throw new ClientHTTPException(SC_BAD_REQUEST, errInfo
160:                            .toString());
161:                } else {
162:                    // No acceptable format was found, send 406 as required by RFC 2616
163:                    throw new ClientHTTPException(SC_NOT_ACCEPTABLE,
164:                            "No acceptable file format found.");
165:                }
166:            }
167:
168:            public static void logAcceptableFormats(HttpServletRequest request) {
169:                Logger logger = LoggerFactory.getLogger(ProtocolUtil.class);
170:                if (logger.isDebugEnabled()) {
171:                    StringBuilder acceptable = new StringBuilder(64);
172:
173:                    @SuppressWarnings("unchecked")
174:                    Enumeration<String> acceptHeaders = request
175:                            .getHeaders(HttpServerUtil.ACCEPT_HEADER_NAME);
176:
177:                    while (acceptHeaders.hasMoreElements()) {
178:                        acceptable.append(acceptHeaders.nextElement());
179:
180:                        if (acceptHeaders.hasMoreElements()) {
181:                            acceptable.append(',');
182:                        }
183:                    }
184:
185:                    logger.debug("Acceptable formats: " + acceptable);
186:                }
187:            }
188:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.