001    /**
002     * Copyright (c) 2000-2013 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            @Override
036            public String getRootPath(SharepointRequest sharepointRequest) {
037                    return StringPool.BLANK;
038            }
039    
040            @Override
041            public void process(SharepointRequest sharepointRequest)
042                    throws SharepointException {
043    
044                    try {
045                            doProcess(sharepointRequest);
046                    }
047                    catch (Exception e) {
048                            throw new SharepointException(e);
049                    }
050            }
051    
052            protected void doProcess(SharepointRequest sharepointRequest)
053                    throws Exception {
054    
055                    ServletResponseUtil.write(
056                            sharepointRequest.getHttpServletResponse(),
057                            getResponse(sharepointRequest, false));
058            }
059    
060            protected abstract List<ResponseElement> getElements(
061                            SharepointRequest sharepointRequest)
062                    throws Exception;
063    
064            protected String getResponse(
065                            SharepointRequest sharepointRequest, boolean appendNewline)
066                    throws Exception {
067    
068                    StringBundler sb = new StringBundler();
069    
070                    sb.append("<html><head><title>vermeer RPC packet</title></head>");
071                    sb.append(StringPool.NEW_LINE);
072                    sb.append("<body>");
073                    sb.append(StringPool.NEW_LINE);
074    
075                    Property property = new Property(
076                            "method", getMethodName() + ":" + SharepointUtil.VERSION);
077    
078                    sb.append(property.parse());
079    
080                    List<ResponseElement> elements = getElements(sharepointRequest);
081    
082                    for (ResponseElement element : elements) {
083                            sb.append(element.parse());
084                    }
085    
086                    sb.append("</body>");
087                    sb.append(StringPool.NEW_LINE);
088                    sb.append("</html>");
089    
090                    if (appendNewline) {
091                            sb.append(StringPool.NEW_LINE);
092                    }
093    
094                    String html = sb.toString();
095    
096                    if (_log.isDebugEnabled()) {
097                            _log.debug("Response HTML\n" + html);
098                    }
099    
100                    return html;
101            }
102    
103            private static Log _log = LogFactoryUtil.getLog(BaseMethodImpl.class);
104    
105    }