001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/help/tags/sakai_2-4-1/help-tool/src/java/org/sakaiproject/tool/help/ContentServlet.java $
003: * $Id: ContentServlet.java 10227 2006-06-06 14:03:38Z marquard@ched.uct.ac.za $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.help;
021:
022: import java.io.BufferedReader;
023: import java.io.IOException;
024: import java.io.InputStreamReader;
025: import java.io.OutputStreamWriter;
026: import java.net.URL;
027:
028: import javax.servlet.ServletException;
029: import javax.servlet.http.HttpServlet;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032:
033: import org.sakaiproject.api.app.help.HelpManager;
034: import org.sakaiproject.api.app.help.Resource;
035: import org.sakaiproject.component.cover.ComponentManager;
036:
037: /**
038: * Content Servlet serves help documents to document frame.
039: * @version $Id: ContentServlet.java 10227 2006-06-06 14:03:38Z marquard@ched.uct.ac.za $
040: */
041: public class ContentServlet extends HttpServlet {
042:
043: private static final String DOC_ID = "docId";
044: private static final String TEXT_HTML = "text/html; charset=UTF-8";
045: private HelpManager helpManager;
046:
047: /**
048: * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
049: */
050: protected void doGet(HttpServletRequest req, HttpServletResponse res)
051: throws ServletException, IOException {
052:
053: getHelpManager().initialize();
054: String docId = req.getParameter(DOC_ID);
055:
056: OutputStreamWriter writer = new OutputStreamWriter(res
057: .getOutputStream(), "UTF-8");
058: res.setContentType(TEXT_HTML);
059:
060: Resource resource = getHelpManager().getResourceByDocId(docId);
061:
062: URL url;
063: if (resource != null) {
064: if (!getHelpManager().getRestConfiguration()
065: .getOrganization().equalsIgnoreCase("sakai")) {
066: writer.write(RestContentProvider
067: .getTransformedDocument(getServletContext(),
068: getHelpManager(), resource));
069: } else if (resource.getLocation().startsWith("/")) {
070: if (!"".equals(getHelpManager().getExternalLocation())) {
071: url = new URL(getHelpManager()
072: .getExternalLocation()
073: + resource.getLocation());
074: } else {
075: url = HelpManager.class.getResource(resource
076: .getLocation());
077: }
078:
079: BufferedReader br = new BufferedReader(
080: new InputStreamReader(url.openStream(), "UTF-8"));
081:
082: int readReturn = 0;
083: String sbuf = new String();
084: while ((sbuf = br.readLine()) != null) {
085: writer.write(sbuf);
086: writer.write(System.getProperty("line.separator"));
087: }
088: br.close();
089: } else {
090: res.sendRedirect(resource.getLocation());
091: }
092: }
093: writer.flush();
094: writer.close();
095: }
096:
097: /**
098: * get the component manager through cover
099: * @return help manager
100: */
101: public HelpManager getHelpManager() {
102: if (helpManager == null) {
103: return (HelpManager) ComponentManager.get(HelpManager.class
104: .getName());
105: }
106: return helpManager;
107: }
108: }
|