001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.httpconnector.handlers;
017:
018: import org.apache.xmlbeans.XmlOptions;
019: import org.outerj.daisy.repository.ChangeType;
020: import org.outerj.daisy.repository.Repository;
021: import org.outerj.daisy.repository.Document;
022: import org.outerj.daisy.repository.VersionState;
023: import org.outerj.daisy.repository.Version;
024: import org.outerj.daisy.httpconnector.spi.HttpUtil;
025: import org.outerj.daisy.httpconnector.spi.BadRequestException;
026: import org.outerj.daisy.httpconnector.spi.RequestHandlerSupport;
027: import org.outerj.daisy.util.HttpConstants;
028: import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
029: import org.outerx.daisy.x10.VersionDocument;
030:
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import java.util.Map;
035:
036: public class VersionHandler extends AbstractRepositoryRequestHandler {
037: public String getPathPattern() {
038: return "/document/*/version/*";
039: }
040:
041: public void handleRequest(Map matchMap, HttpServletRequest request,
042: HttpServletResponse response, Repository repository,
043: RequestHandlerSupport support) throws Exception {
044: String documentId = (String) matchMap.get("1");
045: long versionId = HttpUtil.parseId("version", (String) matchMap
046: .get("2"));
047: long branchId = HttpUtil.getBranchId(request, repository);
048: long languageId = HttpUtil.getLanguageId(request, repository);
049:
050: if (request.getMethod().equals(HttpConstants.GET)) {
051: Document document = repository.getDocument(documentId,
052: branchId, languageId, false);
053: document.getVersion(versionId).getXml().save(
054: response.getOutputStream());
055: } else if (request.getMethod().equals(HttpConstants.POST)) {
056: String action = request.getParameter("action");
057: if ("changeState".equals(action)) {
058: String newState = request.getParameter("newState");
059: if (newState == null)
060: throw new BadRequestException(
061: "Missing parameter \"newState\".");
062: VersionState versionState = VersionState
063: .fromString(newState);
064: Document document = repository.getDocument(documentId,
065: branchId, languageId, true);
066: Version version = document.getVersion(versionId);
067: version.setState(versionState);
068: version.save();
069: version.getShallowXml()
070: .save(response.getOutputStream());
071: } else if (action == null) {
072: XmlOptions xmlOptions = new XmlOptions()
073: .setLoadUseXMLReader(LocalSAXParserFactory
074: .newXmlReader());
075: VersionDocument versionDocument = VersionDocument.Factory
076: .parse(request.getInputStream(), xmlOptions);
077: VersionDocument.Version versionXml = versionDocument
078: .getVersion();
079: Document document = repository.getDocument(documentId,
080: branchId, languageId, true);
081: Version version = document.getVersion(versionId);
082: version.setState(VersionState.fromString(versionXml
083: .getState()));
084: if (versionXml.isSetSyncedWithLanguageId()
085: && versionXml.isSetSyncedWithVersionId())
086: version.setSyncedWith(versionXml
087: .getSyncedWithLanguageId(), versionXml
088: .getSyncedWithVersionId());
089: else
090: version.setSyncedWith(null);
091: version.setChangeType(ChangeType.fromString(versionXml
092: .getChangeType()));
093: version.setChangeComment(versionXml.getChangeComment());
094: version.save();
095: version.getShallowXml()
096: .save(response.getOutputStream());
097: } else {
098: throw new BadRequestException(
099: "Unsupported value for \"action\" parameter: "
100: + action);
101: }
102: } else {
103: response.sendError(HttpConstants._405_Method_Not_Allowed);
104: }
105: }
106: }
|