Source Code Cross Referenced for FilePublisher.java in  » GIS » GeoServer » org » geoserver » ows » 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 » GIS » GeoServer » org.geoserver.ows 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002:         * This code is licensed under the GPL 2.0 license, availible at the root
003:         * application directory.
004:         */
005:        package org.geoserver.ows;
006:
007:        import java.io.File;
008:        import java.io.FileInputStream;
009:        import java.io.OutputStream;
010:
011:        import javax.servlet.http.HttpServletRequest;
012:        import javax.servlet.http.HttpServletResponse;
013:
014:        import org.geoserver.ows.util.EncodingInfo;
015:        import org.geoserver.ows.util.XmlCharsetDetector;
016:        import org.geoserver.platform.GeoServerResourceLoader;
017:        import org.springframework.web.servlet.ModelAndView;
018:        import org.springframework.web.servlet.mvc.AbstractController;
019:
020:        /**
021:         * Controller which publishes files through a web interface.
022:         * <p>
023:         * To use this controller, it should be mapped to a particular url in the url
024:         * mapping of the spring dispatcher servlet. Example:
025:         * <pre>
026:         * <code>
027:         *   &lt;bean id="filePublisher" class="org.geoserver.ows.FilePublisher"/&gt;
028:         *   &lt;bean id="dispatcherMappings"
029:         *      &lt;property name="alwaysUseFullPath" value="true"/&gt;
030:         *      class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt;
031:         *      &lt;property name="mappings"&gt;
032:         *        &lt;prop key="/schemas/** /*.xsd"&gt;filePublisher&lt;/prop&gt;
033:         *        &lt;prop key="/schemas/** /*.dtd"&gt;filePublisher&lt;/prop&gt;
034:         *        &lt;prop key="/styles/*"&gt;filePublisher&lt;/prop&gt;
035:         *      &lt;/property&gt;
036:         *   &lt;/bean&gt;
037:         * </code>
038:         * </pre>
039:         * </p>
040:         *
041:         * @author Justin Deoliveira, The Open Planning Project
042:         *
043:         */
044:        public class FilePublisher extends AbstractController {
045:            /**
046:             * Resource loader
047:             */
048:            protected GeoServerResourceLoader loader;
049:
050:            /**
051:             * Creates the new file publisher.
052:             *
053:             * @param loader The loader used to locate files.
054:             */
055:            public FilePublisher(GeoServerResourceLoader loader) {
056:                this .loader = loader;
057:            }
058:
059:            protected ModelAndView handleRequestInternal(
060:                    HttpServletRequest request, HttpServletResponse response)
061:                    throws Exception {
062:                String ctxPath = request.getContextPath();
063:                String reqPath = request.getRequestURI();
064:                reqPath = reqPath.substring(ctxPath.length());
065:
066:                if ((reqPath.length() > 1) && reqPath.startsWith("/")) {
067:                    reqPath = reqPath.substring(1);
068:                }
069:
070:                // sigh, in order to serve the file we have to open it 2 times
071:                // 1) to determine its mime type
072:                // 2) to determine its encoding and really serve it
073:                // we can't coalish 1) because we don't have a way to give jmimemagic the bytes at the 
074:                // beginning of the file without disabling extension quick matching
075:
076:                //load the file
077:                File file = loader.find(reqPath);
078:
079:                if (file == null) {
080:                    //return a 404
081:                    response.sendError(HttpServletResponse.SC_NOT_FOUND);
082:
083:                    return null;
084:                }
085:
086:                String mime = getServletContext().getMimeType(file.getName());
087:                if (mime == null) {
088:                    //return a 415: Unsupported Media Type
089:                    response
090:                            .sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
091:
092:                    return null;
093:                }
094:                response.setContentType(mime);
095:
096:                // Guessing the charset (and closing the stream)
097:                EncodingInfo encInfo = null;
098:                FileInputStream input = null;
099:                OutputStream output = null;
100:                final byte[] b4 = new byte[4];
101:                int count = 0;
102:                try {
103:                    // open the output
104:                    input = new FileInputStream(file);
105:
106:                    // Read the first four bytes, and determine charset encoding
107:                    count = input.read(b4);
108:                    encInfo = XmlCharsetDetector.getEncodingName(b4, count);
109:                    response
110:                            .setCharacterEncoding(encInfo.getEncoding() != null ? encInfo
111:                                    .getEncoding()
112:                                    : "UTF-8");
113:
114:                    // send out the first four bytes read
115:                    output = response.getOutputStream();
116:                    output.write(b4, 0, count);
117:
118:                    // copy the content to the output
119:                    byte[] buffer = new byte[8192];
120:                    int n = -1;
121:                    while ((n = input.read(buffer)) != -1) {
122:                        output.write(buffer, 0, n);
123:                    }
124:                } finally {
125:                    if (output != null)
126:                        output.flush();
127:                    if (input != null)
128:                        input.close();
129:                }
130:
131:                return null;
132:            }
133:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.