001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.sharepoint.methods;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.sharepoint.ResponseElement;
019    import com.liferay.portal.sharepoint.SharepointException;
020    import com.liferay.portal.sharepoint.SharepointRequest;
021    import com.liferay.portal.sharepoint.SharepointUtil;
022    import com.liferay.util.servlet.ServletResponseUtil;
023    
024    import java.util.List;
025    
026    /**
027     * @author Bruno Farache
028     */
029    public abstract class BaseMethodImpl implements Method {
030    
031            public String getRootPath(SharepointRequest sharepointRequest) {
032                    return StringPool.BLANK;
033            }
034    
035            public void process(SharepointRequest sharepointRequest)
036                    throws SharepointException {
037    
038                    try {
039                            doProcess(sharepointRequest);
040                    }
041                    catch (Exception e) {
042                            throw new SharepointException(e);
043                    }
044            }
045    
046            protected abstract List<ResponseElement> getElements(
047                            SharepointRequest sharepointRequest)
048                    throws Exception;
049    
050            protected void doProcess(SharepointRequest sharepointRequest)
051                    throws Exception {
052    
053                    ServletResponseUtil.write(
054                            sharepointRequest.getHttpServletResponse(),
055                            getResponseBuffer(sharepointRequest).toString());
056            }
057    
058            protected StringBuilder getResponseBuffer(
059                            SharepointRequest sharepointRequest)
060                    throws Exception {
061    
062                    StringBuilder sb = new StringBuilder();
063    
064                    SharepointUtil.addTop(sb, getMethodName());
065    
066                    List<ResponseElement> elements = getElements(sharepointRequest);
067    
068                    for (ResponseElement element : elements) {
069                            sb.append(element.parse());
070                    }
071    
072                    SharepointUtil.addBottom(sb);
073    
074                    return sb;
075            }
076    
077    }