Package com.google.code.greenwood.confluenceadvancedcodeblockplugin.servlets

Source Code of com.google.code.greenwood.confluenceadvancedcodeblockplugin.servlets.DownloadListCodeServlet

/******************************************************************************
*  Copyright 2013 Bernhard Grünewaldt                                        *
*                                                                            *
*  Licensed under the Apache License, Version 2.0 (the "License");           *
*  you may not use this file except in compliance with the License.          *
*  You may obtain a copy of the License at                                   *
*                                                                            *
*      http://www.apache.org/licenses/LICENSE-2.0                            *
*                                                                            *
*  Unless required by applicable law or agreed to in writing, software       *
*  distributed under the License is distributed on an "AS IS" BASIS,         *
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
*  See the License for the specific language governing permissions and       *
*  limitations under the License.                                            *
******************************************************************************/
package com.google.code.greenwood.confluenceadvancedcodeblockplugin.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.atlassian.confluence.core.ContentPropertyManager;
import com.atlassian.confluence.pages.Page;
import com.atlassian.confluence.pages.PageManager;
import com.atlassian.confluence.setup.settings.SettingsManager;
import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
import com.atlassian.user.User;
import com.google.code.greenwood.confluenceadvancedcodeblockplugin.macro.AdvancedCodeBlockMacro;

public class DownloadListCodeServlet extends HttpServlet {

  private static final Logger log = LoggerFactory.getLogger(DownloadListCodeServlet.class);

  private static final long serialVersionUID = 3876936240938132354L;

  public static final String PARAM_PAGEID = "pageid";
 
  private ContentPropertyManager contentPropertyManager;
  private SettingsManager settingsManager;
 
  private PageManager pageManager;

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("application/xml");
      PrintWriter out = response.getWriter();
     
 
    String baseUrl = settingsManager.getGlobalSettings().getBaseUrl();

   
   
      User user = AuthenticatedUserThreadLocal.getUser();
    if (user == null) {
      response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        out.println("410 unauthorized");
        return;
    }
   
    try {
      Long pageid = new Long(request.getParameter(PARAM_PAGEID));   
      Page page = pageManager.getPage(pageid);
      String unparsedPagePropertyKeyList = contentPropertyManager.getTextProperty(page, AdvancedCodeBlockMacro.PAGE_PROPERTY_PREFIX+pageid);
      List<String> pagePropertyKeyList = parseAllPagePropertyKeys(unparsedPagePropertyKeyList);
      out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
      out.print("<advancedcodeblockmacro>\n");
      out.print("<pageinfo>\n");
      out.print("  <pageid>"+pageid+"</pageid>\n");
      out.print("  <pagename>"+page.getTitle()+"</pagename>\n");
      out.print("  <spacekey>"+page.getSpaceKey()+"</spacekey>\n");
      out.print("</pageinfo>\n");
      out.print("<pagepropertykeys>\n");
      for (String pagepropertykey : pagePropertyKeyList) {
        out.print("  <pageproperty>\n");
        out.print("    <pagepropertykey>"+pagepropertykey+"</pagepropertykey>\n");
        String pagepropertykeyWithoutPrefix = pagepropertykey.replaceFirst(AdvancedCodeBlockMacro.PAGE_PROPERTY_PREFIX, "");       
        byte[] decoded = Base64.decodeBase64(pagepropertykeyWithoutPrefix);
        out.print("    <pagepropertykeyunencoded>"+new String(decoded, "UTF-8")+"</pagepropertykeyunencoded>\n");
        out.print("    <downloadurl>"+baseUrl+"/plugins/servlet/advanced-codeblock-macro-downloadcode?pagepropertykey="+pagepropertykeyWithoutPrefix+"&amp;pageid="+pageid+"</downloadurl>\n");
        out.print("  </pageproperty>\n");
      }
      out.print("</pagepropertykeys>\n");
      out.print("</advancedcodeblockmacro>\n");

    } catch (Exception e) {
      log.error("unexpected exception occured.", e);
      response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        out.println("500 error. Did you provide valid URL-Parameters? Check the logfile.");
    }
  }

 
  public void setContentPropertyManager(
      ContentPropertyManager contentPropertyManager) {
    this.contentPropertyManager = contentPropertyManager;
  }

  public void setPageManager(PageManager pageManager) {
    this.pageManager = pageManager;
  }

 
  public void setSettingsManager(SettingsManager settingsManager) {
    this.settingsManager = settingsManager;
  }


  /**
   * Parses the PagePropertyKeys added by AdvancedCodeBlockMacro#addPagePropertyKeyToPagePropertyList() into String List.
   * @see AdvancedCodeBlockMacro#addPagePropertyKeyToPagePropertyList()
   * @param unparsedPagePropertyKeyList
   * @return
   */
  public List<String> parseAllPagePropertyKeys(String unparsedPagePropertyKeyList) {
    if (unparsedPagePropertyKeyList != null && ! unparsedPagePropertyKeyList.isEmpty()) {
      String[] splitted = unparsedPagePropertyKeyList.split("[|]");
      return new ArrayList<String>(Arrays.asList(splitted));
    }
    return new ArrayList<String>();
  }
 
  public String decodePropertyKey(String base64EncodedPropertyKey) throws UnsupportedEncodingException {
    byte[] decoded = Base64.decodeBase64(base64EncodedPropertyKey);
    return new String(decoded, "UTF-8");
  }
}

TOP

Related Classes of com.google.code.greenwood.confluenceadvancedcodeblockplugin.servlets.DownloadListCodeServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.