001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.sharepoint.methods;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.sharepoint.Property;
023    import com.liferay.portal.sharepoint.ResponseElement;
024    import com.liferay.portal.sharepoint.SharepointException;
025    import com.liferay.portal.sharepoint.SharepointRequest;
026    import com.liferay.portal.sharepoint.SharepointUtil;
027    
028    import java.util.List;
029    
030    /**
031     * @author Bruno Farache
032     */
033    public abstract class BaseMethodImpl implements Method {
034    
035            public String getRootPath(SharepointRequest sharepointRequest) {
036                    return StringPool.BLANK;
037            }
038    
039            public void process(SharepointRequest sharepointRequest)
040                    throws SharepointException {
041    
042                    try {
043                            doProcess(sharepointRequest);
044                    }
045                    catch (Exception e) {
046                            throw new SharepointException(e);
047                    }
048            }
049    
050            protected void doProcess(SharepointRequest sharepointRequest)
051                    throws Exception {
052    
053                    ServletResponseUtil.write(
054                            sharepointRequest.getHttpServletResponse(),
055                            getResponse(sharepointRequest, false));
056            }
057    
058            protected abstract List<ResponseElement> getElements(
059                            SharepointRequest sharepointRequest)
060                    throws Exception;
061    
062            protected String getResponse(
063                            SharepointRequest sharepointRequest, boolean appendNewline)
064                    throws Exception {
065    
066                    StringBundler sb = new StringBundler();
067    
068                    sb.append("<html><head><title>vermeer RPC packet</title></head>");
069                    sb.append(StringPool.NEW_LINE);
070                    sb.append("<body>");
071                    sb.append(StringPool.NEW_LINE);
072    
073                    Property property = new Property(
074                            "method", getMethodName() + ":" + SharepointUtil.VERSION);
075    
076                    sb.append(property.parse());
077    
078                    List<ResponseElement> elements = getElements(sharepointRequest);
079    
080                    for (ResponseElement element : elements) {
081                            sb.append(element.parse());
082                    }
083    
084                    sb.append("</body>");
085                    sb.append(StringPool.NEW_LINE);
086                    sb.append("</html>");
087    
088                    if (appendNewline) {
089                            sb.append(StringPool.NEW_LINE);
090                    }
091    
092                    String html = sb.toString();
093    
094                    if (_log.isDebugEnabled()) {
095                            _log.debug("Response HTML\n" + html);
096                    }
097    
098                    return html;
099            }
100    
101            private static Log _log = LogFactoryUtil.getLog(BaseMethodImpl.class);
102    
103    }