Source Code Cross Referenced for RESTCallingConvention.java in  » Web-Services » xins » com » mycompany » rest » api » 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 » xins » com.mycompany.rest.api 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: RESTCallingConvention.java,v 1.4 2007/09/18 11:27:16 agoubard Exp $
003:         */
004:        package com.mycompany.rest.api;
005:
006:        import java.io.IOException;
007:        import java.io.Writer;
008:        import java.util.Iterator;
009:        import java.util.StringTokenizer;
010:
011:        import javax.servlet.http.HttpServletRequest;
012:        import javax.servlet.http.HttpServletResponse;
013:
014:        import org.xins.common.MandatoryArgumentChecker;
015:        import org.xins.common.Utils;
016:        import org.xins.common.collections.BasicPropertyReader;
017:        import org.xins.common.spec.EntityNotFoundException;
018:        import org.xins.common.spec.InvalidSpecificationException;
019:
020:        import org.xins.server.API;
021:        import org.xins.server.CallResultOutputter;
022:        import org.xins.server.CustomCallingConvention;
023:        import org.xins.server.FunctionNotSpecifiedException;
024:        import org.xins.server.FunctionRequest;
025:        import org.xins.server.FunctionResult;
026:        import org.xins.server.InvalidRequestException;
027:
028:        /**
029:         * The REST calling convention as describe at http://en.wikipedia.org/wiki/REST.
030:         *
031:         * @version $Revision: 1.4 $ $Date: 2007/09/18 11:27:16 $
032:         * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
033:         */
034:        public class RESTCallingConvention extends CustomCallingConvention {
035:
036:            /**
037:             * The response encoding format.
038:             */
039:            static final String RESPONSE_ENCODING = "UTF-8";
040:
041:            /**
042:             * The content type of the HTTP response.
043:             */
044:            static final String RESPONSE_CONTENT_TYPE = "text/xml; charset="
045:                    + RESPONSE_ENCODING;
046:
047:            /**
048:             * The API using this calling convention.
049:             */
050:            private final API _api;
051:
052:            /**
053:             * Constructs a new <code>addUserImpl</code> instance.
054:             *
055:             * @param api
056:             *    the API, using this calling convention, cannot be <code>null</code>.
057:             *
058:             * @throws IllegalArgumentException
059:             *    if <code>api == null</code>.
060:             */
061:            public RESTCallingConvention(API api)
062:                    throws IllegalArgumentException {
063:                MandatoryArgumentChecker.check("api", api);
064:                _api = api;
065:            }
066:
067:            protected String[] getSupportedMethods() {
068:                return new String[] { "GET", "POST", "PUT", "DELETE" };
069:            }
070:
071:            protected boolean matches(HttpServletRequest httpRequest)
072:                    throws Exception {
073:
074:                return httpRequest.getQueryString() == null;
075:            }
076:
077:            protected FunctionRequest convertRequestImpl(
078:                    HttpServletRequest httpRequest)
079:                    throws InvalidRequestException,
080:                    FunctionNotSpecifiedException {
081:                String restMethod = "";
082:                String httpMethod = httpRequest.getMethod().toLowerCase();
083:                if (httpMethod.equals("post")) {
084:                    restMethod = "Update";
085:                } else if (httpMethod.equals("put")) {
086:                    restMethod = "Add";
087:                } else if (httpMethod.equals("get")) {
088:                    restMethod = "Get";
089:                } else if (httpMethod.equals("delete")) {
090:                    restMethod = "Delete";
091:                } else {
092:                    throw Utils.logProgrammingError("Unauthorized method: "
093:                            + httpMethod);
094:                }
095:                String path = httpRequest.getContextPath();
096:
097:                // Get the first path to know the resource wanted
098:                String resourceName = "";
099:                StringTokenizer stPaths = new StringTokenizer(path, "/");
100:                if (stPaths.hasMoreTokens()) {
101:                    resourceName = stPaths.nextToken();
102:                }
103:                String functionName = restMethod
104:                        + resourceName.substring(0, 1).toUpperCase()
105:                        + resourceName.substring(1);
106:                if (functionName.endsWith("s")) {
107:                    functionName = functionName.substring(0, functionName
108:                            .length() - 1);
109:                }
110:
111:                // Get the arguments of the functions
112:                BasicPropertyReader params = new BasicPropertyReader();
113:                try {
114:                    Iterator itInputParameters = _api.getAPISpecification()
115:                            .getFunction(functionName).getInputParameters()
116:                            .keySet().iterator();
117:                    while (itInputParameters.hasNext()
118:                            && stPaths.hasMoreTokens()) {
119:                        String nextParamName = (String) itInputParameters
120:                                .next();
121:                        String nextArg = stPaths.nextToken();
122:                        params.set(nextParamName, nextArg);
123:                    }
124:                } catch (InvalidSpecificationException isex) {
125:                    Utils.logIgnoredException(isex);
126:                } catch (EntityNotFoundException enfex) {
127:                    throw new FunctionNotSpecifiedException();
128:                }
129:                return new FunctionRequest(functionName, params, null);
130:            }
131:
132:            protected void convertResultImpl(FunctionResult xinsResult,
133:                    HttpServletResponse httpResponse,
134:                    HttpServletRequest httpRequest) throws IOException {
135:
136:                // Set the status code and the content type
137:                httpResponse.setStatus(HttpServletResponse.SC_OK);
138:                httpResponse.setContentType(RESPONSE_CONTENT_TYPE);
139:
140:                Writer out = httpResponse.getWriter();
141:                CallResultOutputter.output(out, xinsResult);
142:                out.close();
143:            }
144:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.