1 16 package org.outerj.daisy.publisher.serverimpl; 17 18 import org.apache.avalon.framework.service.Serviceable; 19 import org.apache.avalon.framework.service.ServiceManager; 20 import org.apache.avalon.framework.service.ServiceException; 21 import org.apache.avalon.framework.logger.LogEnabled; 22 import org.apache.avalon.framework.logger.Logger; 23 import org.apache.avalon.framework.activity.Disposable; 24 import org.apache.avalon.framework.activity.Initializable; 25 import org.apache.xmlbeans.XmlOptions; 26 import org.outerj.daisy.publisher.Publisher; 27 import org.outerj.daisy.publisher.BlobInfo; 28 import org.outerj.daisy.httpconnector.*; 29 import org.outerj.daisy.repository.Repository; 30 import org.outerj.daisy.repository.VariantKey; 31 import org.outerj.daisy.xmlutil.XmlSerializer; 32 import org.outerj.daisy.xmlutil.LocalSAXParserFactory; 33 import org.mortbay.http.handler.AbstractHttpHandler; 34 import org.mortbay.http.HttpRequest; 35 import org.mortbay.http.HttpResponse; 36 import org.mortbay.http.HttpException; 37 import org.xml.sax.ContentHandler ; 38 import org.outerx.daisy.x10Publisher.*; 39 40 import java.io.IOException ; 41 import java.io.OutputStream ; 42 import java.io.InputStream ; 43 44 47 public class PublisherHttpConnector implements Serviceable, Initializable, Disposable, LogEnabled { 48 private HttpConnector httpConnector; 49 private PublisherHttpHandler httpHandler = new PublisherHttpHandler(); 50 private Logger requestErrorsLogger; 51 private static final int BUFFER_SIZE = 32768; 52 53 56 public void service(ServiceManager serviceManager) throws ServiceException { 57 httpConnector = (HttpConnector)serviceManager.lookup("httpconnector"); 58 } 59 60 public void enableLogging(Logger logger) { 61 this.requestErrorsLogger = logger.getChildLogger("request-errors"); 62 } 63 64 public void initialize() throws Exception { 65 httpConnector.addHandler(httpHandler); 66 } 67 68 public void dispose() { 69 httpConnector.removeHandler(httpHandler); 70 } 71 72 private class PublisherHttpHandler extends AbstractHttpHandler { 73 public void handle(String pathInContext, String pathParams, HttpRequest request, HttpResponse response) 74 throws HttpException, IOException { 75 String path = request.getPath(); 76 try { 77 Repository repository = ((DaisyUserPrincipal)request.getUserPrincipal()).getRepository(); 78 79 if (path.equals("/publisher/blob")) { 80 long documentId = HttpUtil.getLongParam(request, "documentId"); 81 long branchId = HttpUtil.getBranchId(request, repository); 82 long languageId = HttpUtil.getLanguageId(request, repository); 83 VariantKey variantKey = new VariantKey(documentId, branchId, languageId); 84 String version = HttpUtil.getStringParam(request, "version"); 85 String partType = HttpUtil.getStringParam(request, "partType"); 86 87 Publisher publisher = (Publisher)repository.getExtension("Publisher"); 88 BlobInfo blobInfo = publisher.getBlobInfo(variantKey, version, partType); 89 90 response.setDateField("Last-Modified", blobInfo.getLastModified()); 91 response.setContentType(blobInfo.getMimeType()); 92 response.setField("Content-Length", String.valueOf(blobInfo.getSize())); 93 94 OutputStream os = response.getOutputStream(); 95 byte[] buffer = new byte[BUFFER_SIZE]; 96 InputStream is = blobInfo.getInputStream(); 97 try { 98 int read; 99 while ((read = is.read(buffer)) != -1) { 100 os.write(buffer, 0, read); 101 } 102 } finally { 103 is.close(); 104 } 105 106 response.commit(); 107 } else if (path.equals("/publisher/request")) { 108 ContentHandler serializer = new XmlSerializer(response.getOutputStream()); 109 110 XmlOptions xmlOptions = new XmlOptions().setLoadUseXMLReader(LocalSAXParserFactory.newXmlReader()); 111 PublisherRequestDocument publisherRequestDocument = PublisherRequestDocument.Factory.parse(request.getInputStream(), xmlOptions); 112 Publisher publisher = (Publisher)repository.getExtension("Publisher"); 113 publisher.processRequest(publisherRequestDocument, serializer); 114 115 response.commit(); 116 } else if (path.equals("/publisher/document")) { 117 long documentId = HttpUtil.getLongParam(request, "documentId"); 121 String locale = HttpUtil.getStringParam(request, "locale"); 122 String version = HttpUtil.getStringParam(request, "version"); 123 boolean includeNavigation = HttpUtil.getBooleanParam(request, "includeNavigation"); 124 long navigationDocId = -1; 125 String activePath = null; 126 boolean contextualized = true; 127 if (includeNavigation) { 128 navigationDocId = HttpUtil.getLongParam(request, "navigationDocId"); 129 activePath = request.getParameter("activePath"); contextualized = HttpUtil.getBooleanParam(request, "contextualized"); 131 } 132 133 String branch = String.valueOf(HttpUtil.getBranchId(request, repository)); 134 String language = String.valueOf(HttpUtil.getLanguageId(request, repository)); 135 136 PublisherRequestDocument publisherRequestDocument = PublisherRequestDocument.Factory.newInstance(); 137 PublisherRequestDocument.PublisherRequest publisherRequest = publisherRequestDocument.addNewPublisherRequest(); 138 publisherRequest.setLocale(locale); 139 DocumentDocument.Document pubDocReq = publisherRequest.addNewDocument(); 140 pubDocReq.setId(documentId); 141 pubDocReq.setBranch(branch); 142 pubDocReq.setLanguage(language); 143 pubDocReq.setVersion(version); 144 pubDocReq.addNewAclInfo(); 145 pubDocReq.addNewSubscriptionInfo(); 146 pubDocReq.addNewShallowAnnotatedVersion(); 147 pubDocReq.addNewAnnotatedDocument(); 148 PreparedDocumentsDocument.PreparedDocuments prepDocReq = pubDocReq.addNewPreparedDocuments(); 149 PreparedDocumentsDocument.PreparedDocuments.Context prepDocReqContext = prepDocReq.addNewContext(); 150 prepDocReqContext.setBranch(branch); 151 prepDocReqContext.setLanguage(language); 152 if (includeNavigation) { 153 VariantKeyType prepDocNav = prepDocReq.addNewNavigationDocument(); 154 prepDocNav.setId(navigationDocId); 155 prepDocNav.setBranch(branch); 156 prepDocNav.setLanguage(language); 157 } 158 pubDocReq.addNewComments(); 159 if (includeNavigation) { 160 NavigationTreeDocument.NavigationTree navigationTreeReq = publisherRequest.addNewNavigationTree(); 161 VariantKeyType navDoc = navigationTreeReq.addNewNavigationDocument(); 162 navDoc.setId(navigationDocId); 163 navDoc.setBranch(branch); 164 navDoc.setLanguage(language); 165 VariantKeyType activeDoc = navigationTreeReq.addNewActiveDocument(); 166 activeDoc.setId(documentId); 167 activeDoc.setBranch(branch); 168 activeDoc.setLanguage(language); 169 if (activePath != null) { 170 navigationTreeReq.setActivePath(activePath); 171 } 172 navigationTreeReq.setContextualized(contextualized); 173 } 174 175 ContentHandler serializer = new XmlSerializer(response.getOutputStream()); 176 177 Publisher publisher = (Publisher)repository.getExtension("Publisher"); 178 publisher.processRequest(publisherRequestDocument, serializer); 179 180 response.commit(); 181 } 182 } catch (BadRequestException e) { 183 HttpUtil.sendCustomError(e.getMessage(), HttpResponse.__400_Bad_Request, response); 185 } catch (Throwable e) { 186 requestErrorsLogger.error("Error processing request " + path, e); 187 HttpUtil.sendCustomError(e, HttpResponse.__202_Accepted, response); 188 } 189 } 190 } 191 } 192 | Popular Tags |