| 1 | # | ||
| 2 | # Web::DataService::Plugin::Dancer.pm | ||
| 3 | # | ||
| 4 | # This plugin provides Web::DataService with the ability to use Dancer.pm as | ||
| 5 | # its "foundation framework". Web::DataService uses the foundation framework | ||
| 6 | # to parse HTTP requests, to marshall and send back HTTP responses, and to | ||
| 7 | # provide configuration information for the data service. | ||
| 8 | # | ||
| 9 | # Other plugins will eventually be developed to fill this same role using | ||
| 10 | # Mojolicious and other frameworks. | ||
| 11 | # | ||
| 12 | # Author: Michael McClennen <mmcclenn@cpan.org> | ||
| 13 | |||
| 14 | |||
| 15 | use strict; | ||
| 16 | |||
| 17 | package Web::DataService::Plugin::Dancer; | ||
| 18 | |||
| 19 | use Carp qw( carp croak ); | ||
| 20 | |||
| 21 | |||
| 22 | sub initialize_plugin { | ||
| 23 | |||
| 24 | Dancer::set(warnings => 0); | ||
| 25 | #Dancer::set(app_handles_errors => 1); | ||
| 26 | } | ||
| 27 | |||
| 28 | |||
| 29 | # The following methods are called with the parameters specified: $ds is a | ||
| 30 | # reference to a data service instance, $request is a reference to the request | ||
| 31 | # instance defined by Web::DataService. If a request object was defined by | ||
| 32 | # the foundation framework, it will be available as $request->{outer}. The | ||
| 33 | # data service instance is also available as $request->{ds}. | ||
| 34 | |||
| 35 | # ============================================================================ | ||
| 36 | |||
| 37 | # read_config ( ds, name, param ) | ||
| 38 | # | ||
| 39 | # This method returns configuration information from the application | ||
| 40 | # configuration file used by the foundation framework. If $param is given, | ||
| 41 | # then return the value of that configuration parameter (if any). This value | ||
| 42 | # is looked up first under the configuration group $name (if given), and if not | ||
| 43 | # found is then looked up directly. | ||
| 44 | # | ||
| 45 | # If $param is not given, then return the configuration group $name if that | ||
| 46 | # was given, or else a hash of the entire set of configuration parameters. | ||
| 47 | |||
| 48 | sub read_config { | ||
| 49 | |||
| 50 | my ($class, $ds) = @_; | ||
| 51 | |||
| 52 | my $config_hash = Dancer::config; | ||
| 53 | $ds->{_config} = $config_hash; | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | # store_request ( outer, inner ) | ||
| 58 | # | ||
| 59 | # Add to the specified "outer" request object a link to our "inner" request | ||
| 60 | # object. | ||
| 61 | |||
| 62 | sub store_inner { | ||
| 63 | |||
| 64 | my ($plugin, $outer, $inner) = @_; | ||
| 65 | |||
| 66 | Dancer::var('wds_request', $inner); | ||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | # retrieve_request ( outer ) | ||
| 71 | # | ||
| 72 | # Return the "inner" link from the specified request object. | ||
| 73 | |||
| 74 | sub retrieve_inner { | ||
| 75 | |||
| 76 | my ($plugin, $outer) = @_; | ||
| 77 | |||
| 78 | return Dancer::var('wds_request'); | ||
| 79 | } | ||
| 80 | |||
| 81 | |||
| 82 | # store_outer ( outer ) | ||
| 83 | # | ||
| 84 | # Store the current 'outer' request object for later use. This is a no-op for | ||
| 85 | # Dancer, since the current 'outer' request object is always available. | ||
| 86 | |||
| 87 | sub store_outer { | ||
| 88 | |||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | # retrieve_outer ( ) | ||
| 93 | # | ||
| 94 | # Return the 'outer' request object for the request being currently handled. | ||
| 95 | |||
| 96 | sub retrieve_outer { | ||
| 97 | |||
| 98 | return Dancer::request; | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | # get_connection ( ) | ||
| 103 | # | ||
| 104 | # This method returns a database connection. If you wish to use it, make sure | ||
| 105 | # that you "use Dancer::Plugin::Database" in your main program. | ||
| 106 | |||
| 107 | sub get_connection { | ||
| 108 | |||
| 109 | return Dancer::Plugin::Database::database(); | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | # get_base_url ( ) | ||
| 114 | # | ||
| 115 | # Return the base URL for the data service. | ||
| 116 | |||
| 117 | sub get_base_url { | ||
| 118 | |||
| 119 | return Dancer::request->base; | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | # get_request_url ( request ) | ||
| 124 | # | ||
| 125 | # Return the full URL that generated the current request | ||
| 126 | |||
| 127 | sub get_request_url { | ||
| 128 | |||
| 129 | return Dancer::request->uri; | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | # get_request_path ( request ) | ||
| 134 | # | ||
| 135 | # Return the request path | ||
| 136 | |||
| 137 | sub get_request_path { | ||
| 138 | |||
| 139 | return Dancer::request->path; | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | # get_http_method ( request ) | ||
| 144 | # | ||
| 145 | # Return the HTTP method associated with this request. | ||
| 146 | |||
| 147 | sub get_http_method { | ||
| 148 | |||
| 149 | return Dancer::request->method; | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | # get_content_type ( request ) | ||
| 154 | # | ||
| 155 | # Return the content type associated with this request. | ||
| 156 | |||
| 157 | sub get_content_type { | ||
| 158 | |||
| 159 | return Dancer::request->content_type; | ||
| 160 | } | ||
| 161 | |||
| 162 | |||
| 163 | # get_params ( request ) | ||
| 164 | # | ||
| 165 | # Return the parameters for the current request. | ||
| 166 | |||
| 167 | sub get_params { | ||
| 168 | |||
| 169 | my ($plugin, $request, @rest) = @_; | ||
| 170 | |||
| 171 | my $params = Dancer::params(@rest); | ||
| 172 | delete $params->{splat}; | ||
| 173 | return $params; | ||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | # get_param ( param ) | ||
| 178 | # | ||
| 179 | # Return the specified raw parameter value for the current request. | ||
| 180 | |||
| 181 | sub get_param { | ||
| 182 | |||
| 183 | my ($plugin, $request, $param) = @_; | ||
| 184 | |||
| 185 | return Dancer::params->{$param}; | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | # get_request_body ( ) | ||
| 190 | # | ||
| 191 | # Return the body of the request, or the empty string if there wasn't one. | ||
| 192 | |||
| 193 | sub get_request_body { | ||
| 194 | |||
| 195 | my ($plugin, $request) = @_; | ||
| 196 | |||
| 197 | return Dancer::request->body(); | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | # set_cors_header ( request, arg ) | ||
| 202 | # | ||
| 203 | # Set the CORS access control header according to the argument. | ||
| 204 | |||
| 205 | sub set_cors_header { | ||
| 206 | |||
| 207 | my ($plugin, $request, $arg) = @_; | ||
| 208 | |||
| 209 | if ( defined $arg && $arg eq '*' ) | ||
| 210 | { | ||
| 211 | Dancer::header "Access-Control-Allow-Origin" => "*"; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | |||
| 216 | # set_content_type ( outer, type ) | ||
| 217 | # | ||
| 218 | # Set the response content type. | ||
| 219 | |||
| 220 | sub set_content_type { | ||
| 221 | |||
| 222 | my ($plugin, $request, $type) = @_; | ||
| 223 | |||
| 224 | Dancer::content_type $type; | ||
| 225 | } | ||
| 226 | |||
| 227 | |||
| 228 | # set_header ( outer, header, value ) | ||
| 229 | # | ||
| 230 | # Set an arbitrary header in the response. | ||
| 231 | |||
| 232 | sub set_header { | ||
| 233 | |||
| 234 | my ($plugin, $request, $header, $value) = @_; | ||
| 235 | |||
| 236 | Dancer::header $header => $value; | ||
| 237 | } | ||
| 238 | |||
| 239 | |||
| 240 | # set_status ( outer, status ) | ||
| 241 | # | ||
| 242 | # Set the response status code. | ||
| 243 | |||
| 244 | sub set_status { | ||
| 245 | |||
| 246 | my ($class, $request, $code) = @_; | ||
| 247 | |||
| 248 | Dancer::status $code; | ||
| 249 | } | ||
| 250 | |||
| 251 | |||
| 252 | # set_body ( outer, body ) | ||
| 253 | # | ||
| 254 | # Set the response body. | ||
| 255 | |||
| 256 | sub set_body { | ||
| 257 | |||
| 258 | my ($class, $request, $body) = @_; | ||
| 259 | |||
| 260 | Dancer::SharedData->response->content($body); | ||
| 261 | } | ||
| 262 | |||
| 263 | |||
| 264 | # file_path ( @components ) | ||
| 265 | # | ||
| 266 | # Concatenate the specified file paths together, in a file-system-independent | ||
| 267 | # manner. | ||
| 268 | |||
| 269 | sub file_path { | ||
| 270 | |||
| 271 | shift; | ||
| 272 | return Dancer::path(@_); | ||
| 273 | } | ||
| 274 | |||
| 275 | |||
| 276 | # file_readable ( filename ) | ||
| 277 | # | ||
| 278 | # Return true if the specified file exists and is readable, false otherwise. | ||
| 279 | |||
| 280 | sub file_readable { | ||
| 281 | |||
| 282 | my $file_name = Dancer::path(Dancer::setting('public'), $_[1]); | ||
| 283 | return -r $file_name; | ||
| 284 | } | ||
| 285 | |||
| 286 | |||
| 287 | # file_exists ( filename ) | ||
| 288 | # | ||
| 289 | # Return true if the specified file exists, false otherwise. | ||
| 290 | |||
| 291 | sub file_exists { | ||
| 292 | |||
| 293 | my $file_name = Dancer::path(Dancer::setting('public'), $_[1]); | ||
| 294 | return -e $file_name; | ||
| 295 | } | ||
| 296 | |||
| 297 | |||
| 298 | # send_file ( outer, filename ) | ||
| 299 | # | ||
| 300 | # Send as the response the contents of the specified file. For Dancer, the path | ||
| 301 | # is always evaluated relative to the 'public' directory. | ||
| 302 | |||
| 303 | sub send_file { | ||
| 304 | |||
| 305 | return Dancer::send_file($_[2]); | ||
| 306 | } | ||
| 307 | |||
| 308 | |||
| 309 | 1; |