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

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

/******************************************************************************
*  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 javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.user.AuthenticatedUserThreadLocal;
import com.atlassian.user.User;
import com.google.code.greenwood.confluenceadvancedcodeblockplugin.macro.AdvancedCodeBlockMacro;
import com.google.common.base.CharMatcher;

public class DownloadCodeServlet extends HttpServlet {

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

  private static final long serialVersionUID = 3876936240938132354L;

  public static final String PARAM_PAGEID = "pageid";
 
  public static final String PARAM_ASCIIONLY = "asciionly";
 
  public static final String PARAM_PAGECONTENTPROPERTYKEY = "pagepropertykey";
 
  public static final String PARAM_RAWDOWNLOAD_PAGECONTENTPROPERTYKEY = "rawdownloadpagepropertykey";

 
 
  private static final String allowedChars = "äöüß";
  private ContentPropertyManager contentPropertyManager;
  private PageManager pageManager;

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      response.setContentType("text/plain");
      PrintWriter out = response.getWriter();
     
      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));   
      String pagepropertykey = request.getParameter(PARAM_PAGECONTENTPROPERTYKEY);
      String asciiOnly = request.getParameter(PARAM_ASCIIONLY);
      Page page = pageManager.getPage(pageid);
      String cached = contentPropertyManager.getTextProperty(page, AdvancedCodeBlockMacro.PAGE_PROPERTY_PREFIX+pagepropertykey);
      if (asciiOnly != null && "true".equals(asciiOnly)) {
        cached = CharMatcher.ASCII.or(CharMatcher.anyOf(allowedChars)).retainFrom(cached);
        log.info("using ascii Only mode. Stripping all non ASCII Chars from content.");
      }
      out.print(cached);
    } 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;
  }

}
TOP

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

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.