Class: WikiServlet

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/soks-servlet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, servlet_settings) ⇒ WikiServlet

Returns a new instance of WikiServlet.



44
45
46
# File 'lib/soks-servlet.rb', line 44

def initialize( server, servlet_settings )
	@server, @settings = server, servlet_settings
end

Instance Attribute Details

#serverObject

Returns the value of attribute server.



42
43
44
# File 'lib/soks-servlet.rb', line 42

def server
  @server
end

#settingsObject

Returns the value of attribute settings.



42
43
44
# File 'lib/soks-servlet.rb', line 42

def settings
  @settings
end

Instance Method Details

#authenticate(request, response) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/soks-servlet.rb', line 80

def authenticate( request, response )
	settings.authenticators.each do |path_regex,authenticator|
		if request.path_info.downcase =~ path_regex
			authenticator.authenticate( request, response )
			break
		end
	end
end

#doDelete(request, response, pagename, person) ⇒ Object



124
125
126
127
# File 'lib/soks-servlet.rb', line 124

def doDelete( request, response, pagename, person )
	wiki.delete( pagename, person )
	redirect( response, pagename )
end

#doRollback(request, response, pagename, person) ⇒ Object



117
118
119
120
121
122
# File 'lib/soks-servlet.rb', line 117

def doRollback( request, response, pagename, person )
	if request.query['revision']
		wiki.rollback( pagename, request.query['revision'].to_i, person )
	end
	redirect( response, pagename )
end

#doSave(request, response, pagename, person) ⇒ Object

All the following methods change the wiki, then redirect



110
111
112
113
114
115
# File 'lib/soks-servlet.rb', line 110

def doSave( request, response, pagename, person )
	pagename = move_page_as_required( request, response, pagename, person )
	content = request.query["content"].to_s.gsub(/\r\n/,"\n")
	wiki.revise( pagename, content, person ) if content
	redirect( response, pagename )
end

#doUpload(request, response, pagename, person) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/soks-servlet.rb', line 129

def doUpload( request, response, pagename, person )
	pagename = move_page_as_required( request, response, pagename, person )
	unless request.query['file'] == ""
		filename = upload_file_data( request.query['file'] )	
		wiki.revise( pagename, filename, person )
	end 
	redirect( response, pagename )
end

#doWiki(request, response, pagename, person) ⇒ Object

A special redirect to allow WikiLink style urls Not sure if used by anyone, so may delete



91
92
93
# File 'lib/soks-servlet.rb', line 91

def doWiki( request, response, pagename, person )
	redirect( response, pagename.gsub(/([a-z])([A-Z])/,'\1 \2') )
end

#renderView(request, response, pagename, view, person) ⇒ Object

This passes any rendering of the page onto the view class



103
104
105
106
# File 'lib/soks-servlet.rb', line 103

def renderView( request, response, pagename, view, person )
	response.body = view_controller.render( pagename, view, person, request.query )
	response['Content-Type'] = settings.content_types[view] || 'text/html'
end

#serveStaticFile(request, response, url_name, view) ⇒ Object

This passes any requests for static files onto a FileHandler



96
97
98
99
100
# File 'lib/soks-servlet.rb', line 96

def serveStaticFile( request, response, url_name, view )
	request.script_name = view
  	request.path_info = "/#{url_name}"
	WEBrick::HTTPServlet::FileHandler.get_instance(@server, settings.static_file_directories[view], true).service(request, response)
end

#service(request, response) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/soks-servlet.rb', line 48

def service( request, response )
	case request.path_info
	
	# Pass some requests directly to static files
	when '/robots.txt', '/favicon.ico'; serveStaticFile( request, response, request.path[1..-1], 'Attachment' )
	
	# If request of the form /verb/pagename then do it
	when /\/(\w+?)\/(.+)/; wiki_service( request, response, $1.capitalize, $2 )
 	
 	# If request of the form /pagename then redirect to /view/pagename
	when /\/(.+)/	; response.set_redirect( WEBrick::HTTPStatus::Found, "#{settings.url}/#{settings.default_view}/#{$1}" )
	
	# If request of the form / then redirect to /view/home%20page
	when "/"		; redirect( response, settings.home_page, settings.default_view )

	end
end

#wiki_service(request, response, verb, url_name) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/soks-servlet.rb', line 66

def wiki_service( request, response, verb, url_name )
	authenticate request, response
	make_username_valid( request )
	if settings.static_file_directories.include? verb
		serveStaticFile( request, response, url_name, verb )	
	elsif self.respond_to?( "do#{verb}" )
		self.send( "do#{verb}", request, response,settings.page_name_for_url_name(url_name), request.user )
		set_cache_settings(response)
	else
		renderView( request, response, settings.page_name_for_url_name(url_name), verb, request.user )
		set_cache_settings(response)
	end
end