atlassian-jruby-example-plugin / src / main / ruby / app / confluence_sinatra_app.rb

require 'sinatra/base'
require 'app/confluence_helper'

Permission = com.atlassian.confluence.security.Permission

class PermissionException < StandardError; end

class ConfluenceSinatraApp < Sinatra::Base

  include Confluence::Helper

  configure do
    enable :logging
    disable :show_exceptions
  end

  before do
    @current_user = current_user
    redirect "#{$context_path}/login.action" unless @current_user
  end

  not_found { erb :not_found }

  error PermissionException do
    @message = env['sinatra.error'].message
    erb :error
  end

  get '/' do
    @spaces = HostApplicationComponentManager.space_manager.get_all_spaces
    erb :index
  end

  get '/wiki/:space/:title' do
    title, space = unescape(params[:title]), params[:space]
    @page = page_manager.get_page(space, title)
    raise Sinatra::NotFound unless @page
    raise PermissionException, "You are not allowed to view this page" unless current_user_has_view_permission(@page)
    @body = replace_links(view_renderer.render(@page), '/display', '/plugins/servlet/sinatra/wiki')
    erb :show_page
  end

  # Expose methods like "escape" so that they can be used in templates
  helpers { include Rack::Utils }

  private
  def current_user_has_view_permission(obj)
    HostApplicationComponentManager.permission_manager.has_permission(current_user, Permission::VIEW, obj)
  end

  def current_user
    Java::ComAtlassianConfluenceUser::AuthenticatedUserThreadLocal.get_user
  end

  def view_renderer
    HostApplicationComponentManager.view_renderer
  end

  def page_manager
    HostApplicationComponentManager.page_manager
  end

end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.