Request   F
last analyzed

Complexity

Total Complexity 126

Size/Duplication

Total Lines 1399
Duplicated Lines 0 %

Test Coverage

Coverage 9.55%

Importance

Changes 12
Bugs 0 Features 0
Metric Value
eloc 225
c 12
b 0
f 0
dl 0
loc 1399
ccs 32
cts 335
cp 0.0955
rs 2
wmc 126

84 Methods

Rating   Name   Duplication   Size   Complexity  
A setBaseModifier() 0 3 1
A getHttpPartInstance() 0 2 1
A getRequestUrl() 0 3 1
A injectContext() 0 3 1
A getDocumentRoot() 0 3 1
A setBodyStream() 0 3 1
A getSessionManager() 0 3 1
A getRequestHandler() 0 3 1
A getResponse() 0 3 1
A getBodyStream() 0 3 1
A getRequestUri() 0 3 1
A getContextPath() 0 3 1
A setVersion() 0 3 1
A injectSessionManager() 0 3 1
A setRequestUri() 0 3 1
C getProposedSessionId() 0 45 13
A injectHttpRequest() 0 3 1
A getServletPath() 0 3 1
A getBodyContent() 0 3 1
A setParameterMap() 0 3 1
A injectServerVars() 0 3 1
A getHttpRequest() 0 3 1
A setContextPath() 0 3 1
A injectHandlers() 0 3 1
A getParameterMap() 0 3 1
A getPart() 0 4 2
A setServletPath() 0 3 1
A getContext() 0 3 1
A getBaseModifier() 0 3 1
A hasParameter() 0 3 1
A getVersion() 0 3 1
A getAuthenticationManager() 0 3 1
A getHandlers() 0 3 1
A injectResponse() 0 3 1
A getParam() 0 3 1
A setDocumentRoot() 0 3 1
A getParts() 0 3 1
A getParameter() 0 5 2
A injectAuthenticationManager() 0 3 1
A addPart() 0 6 2
A setRequestUrl() 0 3 1
A __construct() 0 35 1
A setAttribute() 0 3 1
A getAttributes() 0 3 1
A init() 0 22 3
A getAttribute() 0 4 2
B prepare() 0 71 6
B getSession() 0 54 9
A getRequestedSessionName() 0 3 1
A getServerName() 0 3 1
A setServerName() 0 3 1
A getCookie() 0 3 1
A getRequestedSessionId() 0 3 1
A setMethod() 0 3 1
A isDispatched() 0 3 1
A login() 0 18 6
A hasHeader() 0 3 1
A addHeader() 0 3 1
A getCookies() 0 3 1
A addCookie() 0 3 1
A hasCookie() 0 3 1
A getServerVar() 0 4 2
A setHeaders() 0 3 1
A setUserPrincipal() 0 3 1
A setAuthType() 0 3 1
A setUri() 0 3 1
A getQueryString() 0 3 1
A setQueryString() 0 3 1
A getMethod() 0 3 1
A setRequestedSessionName() 0 3 1
A getRemoteUser() 0 4 2
A setPathInfo() 0 3 1
A isUserInRole() 0 10 2
A getUri() 0 3 1
A getAuthType() 0 3 1
A getPathInfo() 0 3 1
A setDispatched() 0 3 1
A authenticate() 0 11 2
A getServerVars() 0 3 1
A logout() 0 13 3
A getHeaders() 0 3 1
A getUserPrincipal() 0 3 1
A setRequestedSessionId() 0 3 1
A getHeader() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Request often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Request, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * \AppserverIo\Appserver\ServletEngine\Http\Request
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/appserver
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Appserver\ServletEngine\Http;
22
23
use AppserverIo\Lang\String;
24
use AppserverIo\Http\HttpProtocol;
25
use AppserverIo\Server\Dictionaries\ServerVars;
26
use AppserverIo\Psr\Context\ContextInterface;
27
use AppserverIo\Psr\HttpMessage\PartInterface;
28
use AppserverIo\Psr\HttpMessage\CookieInterface;
29
use AppserverIo\Psr\HttpMessage\RequestInterface;
30
use AppserverIo\Psr\Security\PrincipalInterface;
31
use AppserverIo\Psr\Servlet\SessionUtils;
32
use AppserverIo\Psr\Servlet\ServletException;
33
use AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface;
34
use AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface;
35
use AppserverIo\Psr\Auth\AuthenticationManagerInterface;
36
use AppserverIo\Appserver\Core\Environment;
37
use AppserverIo\Appserver\Core\Utilities\EnvironmentKeys;
38
use AppserverIo\Appserver\ServletEngine\SessionManagerInterface;
39
40
/**
41
 * A Http servlet request implementation.
42
 *
43
 * @author    Tim Wagner <[email protected]>
44
 * @copyright 2015 TechDivision GmbH <[email protected]>
45
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
46
 * @link      https://github.com/appserver-io/appserver
47
 * @link      http://www.appserver.io
48
 */
49
class Request implements HttpServletRequestInterface, ContextInterface
50
{
51
52
    /**
53
     * The path (URI) to the servlet.
54
     *
55
     * @var string
56
     */
57
    protected $servletPath;
58
59
    /**
60
     * The request context.
61
     *
62
     * @var \AppserverIo\Appserver\ServletEngine\Http\RequestContextInterface
63
     */
64
    protected $requestHandler;
65
66
    /**
67
     * The request URI.
68
     *
69
     * @var string
70
     */
71
    protected $requestUri;
72
73
    /**
74
     * The request URL.
75
     *
76
     * @var string
77
     */
78
    protected $requestUrl;
79
80
    /**
81
     * The new session name.
82
     *
83
     * @var string
84
     */
85
    protected $requestedSessionName;
86
87
    /**
88
     * The new session-ID.
89
     *
90
     * @var string
91
     */
92
    protected $requestedSessionId;
93
94
    /**
95
     * The servlet response instance.
96
     *
97
     * @var \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface
98
     */
99
    protected $response;
100
101
    /**
102
     * The server name.
103
     *
104
     * @var string
105
     */
106
    protected $serverName;
107
108
    /**
109
     * The query string.
110
     *
111
     * @var string
112
     */
113
    protected $queryString;
114
115
    /**
116
     * The absolute path info.
117
     *
118
     * @var string
119
     */
120
    protected $pathInfo;
121
122
    /**
123
     * The document root.
124
     *
125
     * @var string
126
     */
127
    protected $documentRoot;
128
129
    /**
130
     * Base modifier which allows for base path generation within rewritten URL environments.
131
     *
132
     * @var string
133
     */
134
    protected $baseModifier;
135
136
    /**
137
     * The body content stream resource.
138
     *
139
     * @var resource
140
     */
141
    protected $bodyStream;
142
143
    /**
144
     * The request context instance.
145
     *
146
     * @var \AppserverIo\Psr\Context\ContextInterface
147
     */
148
    protected $context;
149
150
    /**
151
     * The application context name.
152
     *
153
     * @var string
154
     */
155
    protected $contextPath;
156
157
    /**
158
     * Whether or not the request has been dispatched.
159
     *
160
     * @var boolean
161
     */
162
    protected $dispatched = false;
163
164
    /**
165
     * The HTTP request instance.
166
     *
167
     * @var \AppserverIo\Psr\HttpMessage\RequestInterface
168
     */
169
    protected $httpRequest;
170
171
    /**
172
     * The server variables.
173
     *
174
     * @var array
175
     */
176
    protected $serverVars = array();
177
178
    /**
179
     * The uploaded part instances.
180
     *
181
     * @var array
182
     */
183
    protected $parts = array();
184
185
    /**
186
     * The available request handlers.
187
     *
188
     * @var array
189
     */
190
    protected $handlers = array();
191
192
    /**
193
     * Array that contains the attributes of this context.
194
     *
195
     * @var array
196
     */
197
    protected $attributes = array();
198
199
    /**
200
     * The user principal of the authenticated user or NULL if the user has not been authenticated.
201
     *
202
     * @var \AppserverIo\Psr\Security\PrincipalInterface
203
     */
204
    protected $userPrincipal;
205
206
    /**
207
     * The request's authentication type.
208
     *
209
     * @var string
210
     */
211
    protected $authType;
212
213
    /**
214
     * The session manager instance.
215
     *
216
     * @var \AppserverIo\Appserver\ServletEngine\SessionManagerInterface
217
     */
218
    protected $sessionManager;
219
220
    /**
221
     * The session manager instance.
222
     *
223
     * @var \AppserverIo\Psr\Auth\AuthenticationManagerInterface
224
     */
225
    protected $authenticationManager;
226
227
    /**
228
     * Initializes the request object with the default properties.
229
     */
230 3
    public function __construct()
231
    {
232
233
        // init body stream
234 3
        $this->bodyStream = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type resource of property $bodyStream.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
235
236
        // the request has not been dispatched initially
237 3
        $this->dispatched = false;
238
239
        // initialize the instances
240 3
        $this->context = null;
241 3
        $this->response = null;
242 3
        $this->httpRequest = null;
243 3
        $this->userPrincipal = null;
244 3
        $this->requestHandler = null;
245 3
        $this->sessionManager = null;
246 3
        $this->authenticationManager = null;
247
248
        // initialize the strings
249 3
        $this->pathInfo = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type AppserverIo\Lang\String of property $pathInfo.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
250 3
        $this->serverName = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type AppserverIo\Lang\String of property $serverName.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
251 3
        $this->requestUri = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type AppserverIo\Lang\String of property $requestUri.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
252 3
        $this->requestUrl = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type AppserverIo\Lang\String of property $requestUrl.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
253 3
        $this->queryString = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type AppserverIo\Lang\String of property $queryString.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
254 3
        $this->servletPath = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type AppserverIo\Lang\String of property $servletPath.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
255 3
        $this->baseModifier = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type AppserverIo\Lang\String of property $baseModifier.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
256 3
        $this->documentRoot = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type AppserverIo\Lang\String of property $documentRoot.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
257 3
        $this->requestedSessionId = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type AppserverIo\Lang\String of property $requestedSessionId.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
258 3
        $this->requestedSessionName = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type AppserverIo\Lang\String of property $requestedSessionName.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
259
260
        // reset the server variables and the parts
261 3
        $this->parts = array();
262 3
        $this->handlers = array();
263 3
        $this->serverVars = array();
264 3
        $this->attributes = array();
265 3
    }
266
267
    /**
268
     * Adds the attribute with the passed name to this context.
269
     *
270
     * @param string $key   The key to add the value with
271
     * @param mixed  $value The value to add to the context
272
     *
273
     * @return void
274
     */
275
    public function setAttribute($key, $value)
276
    {
277
        $this->attributes[$key] = $value;
278
    }
279
280
    /**
281
     * Returns the value with the passed name from the context.
282
     *
283
     * @param string $key The key of the value to return from the context.
284
     *
285
     * @return mixed The requested attribute
286
     * @see \AppserverIo\Psr\Context\ContextInterface::getAttribute($key)
287
     */
288
    public function getAttribute($key)
289
    {
290
        if (isset($this->attributes[$key])) {
291
            return $this->attributes[$key];
292
        }
293
    }
294
295
    /**
296
     * Returns the attributes from the context.
297
     *
298
     * @return array The array with the attributes
299
     */
300
    public function getAttributes()
301
    {
302
        return $this->attributes;
303
    }
304
305
    /**
306
     * Initializes the servlet request with the data from the injected HTTP request instance.
307
     *
308
     * @return void
309
     */
310
    public function init()
311
    {
312
313
        // reset the servlet request
314
        $httpRequest = $this->getHttpRequest();
315
316
        // initialize the parts
317
        foreach ($httpRequest->getParts() as $part) {
318
            $this->addPart(Part::fromHttpRequest($part));
319
        }
320
321
        // set the body content if we can find one
322
        if ($httpRequest->getHeader(HttpProtocol::HEADER_CONTENT_LENGTH) > 0) {
323
            $this->setBodyStream($httpRequest->getBodyContent());
324
        }
325
326
        // copy server variables to members
327
        $this->setServerName($this->getServerVar(ServerVars::SERVER_NAME));
0 ignored issues
show
Bug introduced by
AppserverIo\Server\Dicti...ServerVars::SERVER_NAME of type string is incompatible with the type AppserverIo\Lang\String expected by parameter $name of AppserverIo\Appserver\Se...Request::getServerVar(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

327
        $this->setServerName($this->getServerVar(/** @scrutinizer ignore-type */ ServerVars::SERVER_NAME));
Loading history...
328
        $this->setQueryString($this->getServerVar(ServerVars::QUERY_STRING));
329
        $this->setRequestUri($this->getServerVar(ServerVars::X_REQUEST_URI));
330
        $this->setDocumentRoot($this->getServerVar(ServerVars::DOCUMENT_ROOT));
331
        $this->setRequestUrl($this->getServerVar(ServerVars::HTTP_HOST) . $this->getServerVar(ServerVars::X_REQUEST_URI));
0 ignored issues
show
Bug introduced by
$this->getServerVar(Apps...verVars::X_REQUEST_URI) of type string is incompatible with the type AppserverIo\Lang\String expected by parameter $requestUrl of AppserverIo\Appserver\Se...equest::setRequestUrl(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

331
        $this->setRequestUrl(/** @scrutinizer ignore-type */ $this->getServerVar(ServerVars::HTTP_HOST) . $this->getServerVar(ServerVars::X_REQUEST_URI));
Loading history...
332
    }
333
334
    /**
335
     * Prepares the request instance.
336
     *
337
     * @return void
338
     * @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if the request can't be prepared, because no file handle exists
339
     */
340
    public function prepare()
341
    {
342
343
        // prepare the context path
344
        $contextPath = str_replace($this->getContext()->getAppBase(), '', $this->getContext()->getWebappPath());
0 ignored issues
show
Bug introduced by
The method getWebappPath() does not exist on AppserverIo\Psr\Context\ContextInterface. It seems like you code against a sub-type of AppserverIo\Psr\Context\ContextInterface such as AppserverIo\Appserver\Application\Application or AppserverIo\Appserver\Pe...ntainer\ServiceRegistry or AppserverIo\Appserver\Core\AbstractManager or AppserverIo\Appserver\As...Container\AspectManager or AppserverIo\Appserver\Se...rdAuthenticationManager or AppserverIo\Appserver\Console\ConsoleManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

344
        $contextPath = str_replace($this->getContext()->getAppBase(), '', $this->getContext()->/** @scrutinizer ignore-call */ getWebappPath());
Loading history...
Bug introduced by
The method getAppBase() does not exist on AppserverIo\Psr\Context\ContextInterface. It seems like you code against a sub-type of AppserverIo\Psr\Context\ContextInterface such as AppserverIo\Appserver\Application\Application or AppserverIo\Appserver\Pe...ntainer\ServiceRegistry or AppserverIo\Appserver\Core\AbstractManager or AppserverIo\Appserver\Se...rdAuthenticationManager or AppserverIo\Appserver\Console\ConsoleManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

344
        $contextPath = str_replace($this->getContext()->/** @scrutinizer ignore-call */ getAppBase(), '', $this->getContext()->getWebappPath());
Loading history...
345
346
        // set the context path
347
        $this->setContextPath($contextPath);
348
349
        // Fixed #735 - Endless Loop for URLs without servlet name
350
        // Load the request URI and query string from the server vars, because we have to
351
        // take care about changes from other modules like directory or rewrite module!
352
        $uri = $this->getRequestUri();
353
        $queryString = $this->getQueryString();
354
355
        // get uri without querystring
356
        $uriWithoutQueryString = str_replace('?' . $queryString, '', $uri);
357
358
        // initialize the path information and the directory to start with
359
        list ($dirname, $basename, $extension) = array_values(pathinfo($uriWithoutQueryString));
360
361
        // make the registered handlers local
362
        $handlers = $this->getHandlers();
363
364
        // descent the directory structure down to find the (almost virtual) servlet file
365
        do {
366
            // bingo we found a (again: almost virtual) servlet file
367
            if (isset($handlers[".$extension"])) {
368
                // prepare the servlet path (we've to take care, because the
369
                // pathinfo() function converts / to \ on Windows OS
370
                if ($dirname === DIRECTORY_SEPARATOR) {
371
                    $servletPath = '/' . $basename;
372
                } else {
373
                    $servletPath = $dirname . '/' . $basename;
374
                }
375
376
                // we set the basename, because this is the servlet path
377
                $this->setServletPath($servletPath);
0 ignored issues
show
Bug introduced by
$servletPath of type string is incompatible with the type AppserverIo\Lang\String expected by parameter $servletPath of AppserverIo\Appserver\Se...quest::setServletPath(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

377
                $this->setServletPath(/** @scrutinizer ignore-type */ $servletPath);
Loading history...
378
379
                // we set the path info, what is the request URI with stripped dir- and basename
380
                $this->setPathInfo(str_replace($servletPath, '', $uriWithoutQueryString));
381
382
                // we've found what we were looking for, so break here
383
                break;
384
            }
385
386
            // break if we finally can't find a servlet to handle the request
387
            if ($dirname === '/') {
388
                throw new ServletException(
389
                    sprintf('Can\'t find a handler for URI %s, either ', $uri)
390
                );
391
            }
392
393
            // descend down the directory tree
394
            list ($dirname, $basename, $extension) = array_values(pathinfo($dirname));
395
396
        } while ($dirname !== false); // stop until we reached the root of the URI
397
398
        // prepare and set the servlet path
399
        $this->setServletPath(str_replace($contextPath, '', $this->getServletPath()));
400
401
        // prepare the base modifier which allows our apps to provide a base URL
402
        $webappsDir = str_replace($this->getContext()->getBaseDirectory(), '', $this->getContext()->getAppBase());
0 ignored issues
show
Bug introduced by
The method getBaseDirectory() does not exist on AppserverIo\Psr\Context\ContextInterface. It seems like you code against a sub-type of AppserverIo\Psr\Context\ContextInterface such as AppserverIo\Appserver\Application\Application or AppserverIo\Appserver\Pe...ntainer\ServiceRegistry or AppserverIo\Appserver\Core\AbstractManager or AppserverIo\Appserver\Se...rdAuthenticationManager or AppserverIo\Appserver\Console\ConsoleManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

402
        $webappsDir = str_replace($this->getContext()->/** @scrutinizer ignore-call */ getBaseDirectory(), '', $this->getContext()->getAppBase());
Loading history...
403
        $relativeRequestPath = strstr($this->getDocumentRoot(), $webappsDir);
404
        $proposedBaseModifier = str_replace(DIRECTORY_SEPARATOR, '/', str_replace($webappsDir, '', $relativeRequestPath));
405
406
        //  prepare the base modifier
407
        if (strpos($proposedBaseModifier, $contextPath) === 0) {
408
            $this->setBaseModifier('');
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type AppserverIo\Lang\String expected by parameter $baseModifier of AppserverIo\Appserver\Se...uest::setBaseModifier(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

408
            $this->setBaseModifier(/** @scrutinizer ignore-type */ '');
Loading history...
409
        } else {
410
            $this->setBaseModifier($contextPath);
411
        }
412
    }
413
414
    /**
415
     * Injects the context that allows access to session and
416
     * server information.
417
     *
418
     * @param \AppserverIo\Psr\Context\ContextInterface $context The request context instance
419
     *
420
     * @return void
421
     */
422
    public function injectContext(ContextInterface $context)
423
    {
424
        $this->context = $context;
425
    }
426
427
    /**
428
     * Injects the server variables.
429
     *
430
     * @param array $serverVars The server variables
431
     *
432
     * @return void
433
     */
434
    public function injectServerVars(array $serverVars)
435
    {
436
        $this->serverVars = $serverVars;
437
    }
438
439
    /**
440
     * Injects the Http request instance.
441
     *
442
     * @param \AppserverIo\Psr\HttpMessage\RequestInterface $httpRequest The Http request instance
443
     *
444
     * @return void
445
     */
446 1
    public function injectHttpRequest(RequestInterface $httpRequest)
447
    {
448 1
        $this->httpRequest = $httpRequest;
449 1
    }
450
451
    /**
452
     * Injects the session manager instance.
453
     *
454
     * @param \AppserverIo\Appserver\ServletEngine\SessionManagerInterface $sessionManager The session manager instance
455
     *
456
     * @return void
457
     */
458
    public function injectSessionManager(SessionManagerInterface $sessionManager)
459
    {
460
        $this->sessionManager = $sessionManager;
461
    }
462
463
    /**
464
     * Return's the session manager instance.
465
     *
466
     * @return \AppserverIo\Appserver\ServletEngine\SessionManagerInterface The session manager instance
467
     */
468
    public function getSessionManager()
469
    {
470
        return $this->sessionManager;
471
    }
472
473
    /**
474
     * Injects the authentication manager instance.
475
     *
476
     * @param \AppserverIo\Psr\Auth\AuthenticationManagerInterface $authenticationManager The authentication manager instance
477
     *
478
     * @return void
479
     */
480
    public function injectAuthenticationManager(AuthenticationManagerInterface $authenticationManager)
481
    {
482
        $this->authenticationManager = $authenticationManager;
483
    }
484
485
    /**
486
     * Return's the authentication manager instance.
487
     *
488
     * @return \AppserverIo\Psr\Auth\AuthenticationManagerInterface The authentication manager instance
489
     */
490
    public function getAuthenticationManager()
491
    {
492
        return $this->authenticationManager;
493
    }
494
495
    /**
496
     * Returns the Http request instance.
497
     *
498
     * @return \AppserverIo\Psr\HttpMessage\RequestInterface The Http request instance
499
     */
500 1
    public function getHttpRequest()
501
    {
502 1
        return $this->httpRequest;
503
    }
504
505
    /**
506
     * Injects the available file handlers registered by the webserver configuration.
507
     *
508
     * @param array $handlers The available file handlers
509
     *
510
     * @return void
511
     */
512
    public function injectHandlers(array $handlers)
513
    {
514
        $this->handlers = $handlers;
515
    }
516
517
    /**
518
     * Returns the available file handlers registered by the webserver configuration.
519
     *
520
     * @return array The file handlers
521
     */
522
    public function getHandlers()
523
    {
524
        return $this->handlers;
525
    }
526
527
    /**
528
     * Returns the base modifier which allows for base path generation within rewritten URL environments
529
     *
530
     * @return string
531
     */
532
    public function getBaseModifier()
533
    {
534
        return $this->baseModifier;
535
    }
536
537
    /**
538
     * Returns the context that allows access to session and
539
     * server information.
540
     *
541
     * @return \AppserverIo\Psr\Context\ContextInterface The request context
542
     */
543
    public function getContext()
544
    {
545
        return $this->context;
546
    }
547
548
    /**
549
     * Returns a part instance
550
     *
551
     * @return \AppserverIo\Psr\HttpMessage\PartInterface
552
     */
553
    public function getHttpPartInstance()
554
    {
555
    }
556
557
    /**
558
     * Returns the context that allows access to session and
559
     * server information.
560
     *
561
     * @return \AppserverIo\Appserver\ServletEngine\Http\RequestContextInterface The request context
562
     */
563
    public function getRequestHandler()
564
    {
565
        return $this->requestHandler;
566
    }
567
568
    /**
569
     * Sets the absolute path to the document root.
570
     *
571
     * @param string $documentRoot The document root
572
     *
573
     * @return void
574
     */
575
    public function setDocumentRoot($documentRoot)
576
    {
577
        return $this->documentRoot = $documentRoot;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->documentRoot = $documentRoot returns the type AppserverIo\Lang\String which is incompatible with the documented return type void.
Loading history...
578
    }
579
580
    /**
581
     * Returns the absolut path to the document root.
582
     *
583
     * @return string The document root
584
     */
585
    public function getDocumentRoot()
586
    {
587
        return $this->documentRoot;
588
    }
589
590
    /**
591
     * Sets the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.
592
     *
593
     * @param string $requestUri The request URI
594
     *
595
     * @return void
596
     */
597
    public function setRequestUri($requestUri)
598
    {
599
        $this->requestUri = $requestUri;
600
    }
601
602
    /**
603
     * Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.
604
     *
605
     * @return string The request URI
606
     */
607
    public function getRequestUri()
608
    {
609
        return $this->requestUri;
610
    }
611
612
    /**
613
     * Sets the URL the client used to make the request.
614
     *
615
     * @param string $requestUrl The request URL
616
     *
617
     * @return void
618
     */
619
    public function setRequestUrl($requestUrl)
620
    {
621
        $this->requestUrl = $requestUrl;
622
    }
623
624
    /**
625
     * Returns the URL the client used to make the request.
626
     *
627
     * @return string The request URL
628
     */
629
    public function getRequestUrl()
630
    {
631
        return $this->requestUrl;
632
    }
633
634
    /**
635
     * Injects the servlet response bound to this request.
636
     *
637
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $response The servlet response instance
638
     *
639
     * @return void
640
     */
641
    public function injectResponse(HttpServletResponseInterface $response)
642
    {
643
        $this->response = $response;
644
    }
645
646
    /**
647
     * Returns the servlet response bound to this request.
648
     *
649
     * @return \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface The response instance
650
     */
651
    public function getResponse()
652
    {
653
        return $this->response;
654
    }
655
656
    /**
657
     * Returns an array with all request parameters.
658
     *
659
     * @param array $parameterMap The array with the request parameters
660
     *
661
     * @return void
662
     */
663
    public function setParameterMap(array $parameterMap)
664
    {
665
        $this->getHttpRequest()->setParams($parameterMap);
0 ignored issues
show
Bug introduced by
The method setParams() does not exist on AppserverIo\Psr\HttpMessage\RequestInterface. It seems like you code against a sub-type of AppserverIo\Psr\HttpMessage\RequestInterface such as AppserverIo\Http\HttpRequest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

665
        $this->getHttpRequest()->/** @scrutinizer ignore-call */ setParams($parameterMap);
Loading history...
666
    }
667
668
    /**
669
     * Returns an array with all request parameters.
670
     *
671
     * @return array The array with the request parameters
672
     */
673
    public function getParameterMap()
674
    {
675
        return $this->getHttpRequest()->getParams();
0 ignored issues
show
Bug introduced by
The method getParams() does not exist on AppserverIo\Psr\HttpMessage\RequestInterface. It seems like you code against a sub-type of AppserverIo\Psr\HttpMessage\RequestInterface such as AppserverIo\Http\HttpRequest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

675
        return $this->getHttpRequest()->/** @scrutinizer ignore-call */ getParams();
Loading history...
676
    }
677
678
    /**
679
     * Return content
680
     *
681
     * @return string $content
682
     */
683
    public function getBodyContent()
684
    {
685
        return $this->getBodyStream();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getBodyStream() returns the type resource which is incompatible with the documented return type AppserverIo\Lang\String.
Loading history...
686
    }
687
688
    /**
689
     * Returns the body stream as a resource.
690
     *
691
     * @return resource The body stream
692
     */
693
    public function getBodyStream()
694
    {
695
        return $this->bodyStream;
696
    }
697
698
    /**
699
     * Set the base modifier
700
     *
701
     * @param string $baseModifier Base modifier which allows for base path generation within rewritten URL environments
702
     *
703
     * @return null
704
     */
705
    public function setBaseModifier($baseModifier)
706
    {
707
        $this->baseModifier = $baseModifier;
708
    }
709
710
    /**
711
     * Resets the stream resource pointing to body content.
712
     *
713
     * @param resource $bodyStream The body content stream resource
714
     *
715
     * @return void
716
     */
717
    public function setBodyStream($bodyStream)
718
    {
719
        $this->bodyStream = $bodyStream;
720
    }
721
722
    /**
723
     * Set protocol version
724
     *
725
     * @param string $version The http protocol version
726
     *
727
     * @return void
728
     */
729
    public function setVersion($version)
730
    {
731
        $this->getHttpRequest()->setVersion($version);
732
    }
733
734
    /**
735
     * Returns protocol version
736
     *
737
     * @return string
738
     */
739
    public function getVersion()
740
    {
741
        return $this->getHttpRequest()->getVersion();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getHttpRequest()->getVersion() returns the type string which is incompatible with the documented return type AppserverIo\Lang\String.
Loading history...
742
    }
743
744
    /**
745
     * Queries whether the request contains a parameter or not.
746
     *
747
     * @param string $name The name of the param we're query for
748
     *
749
     * @return boolean TRUE if the parameter is available, else FALSE
750
     */
751 1
    public function hasParameter($name)
752
    {
753 1
        return $this->getHttpRequest()->hasParam($name);
0 ignored issues
show
Bug introduced by
The method hasParam() does not exist on AppserverIo\Psr\HttpMessage\RequestInterface. It seems like you code against a sub-type of AppserverIo\Psr\HttpMessage\RequestInterface such as AppserverIo\Http\HttpRequest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

753
        return $this->getHttpRequest()->/** @scrutinizer ignore-call */ hasParam($name);
Loading history...
754
    }
755
756
    /**
757
     * Returns the parameter with the passed name if available or null
758
     * if the parameter not exists.
759
     *
760
     * @param string $name The name of the parameter to return
761
     *
762
     * @return string|null The requested value
763
     */
764
    public function getParam($name)
765
    {
766
        return $this->getHttpRequest()->getParam($name);
0 ignored issues
show
Bug introduced by
The method getParam() does not exist on AppserverIo\Psr\HttpMessage\RequestInterface. It seems like you code against a sub-type of AppserverIo\Psr\HttpMessage\RequestInterface such as AppserverIo\Http\HttpRequest or AppserverIo\Appserver\ServletEngine\Http\Request. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

766
        return $this->getHttpRequest()->/** @scrutinizer ignore-call */ getParam($name);
Loading history...
767
    }
768
769
    /**
770
     * Returns the parameter with the passed name if available or null
771
     * if the parameter not exists.
772
     *
773
     * @param string  $name   The name of the parameter to return
774
     * @param integer $filter The filter to use
775
     *
776
     * @return string|null
777
     */
778
    public function getParameter($name, $filter = FILTER_SANITIZE_STRING)
779
    {
780
        $parameterMap = $this->getParameterMap();
781
        if (isset($parameterMap[$name])) {
782
            return filter_var($parameterMap[$name], $filter);
783
        }
784
    }
785
786
    /**
787
     * Returns a part object by given name
788
     *
789
     * @param string $name The name of the form part
790
     *
791
     * @return \AppserverIo\Http\HttpPart
792
     */
793
    public function getPart($name)
794
    {
795
        if (isset($this->parts[$name])) {
796
            return $this->parts[$name];
797
        }
798
    }
799
800
    /**
801
     * Returns the parts collection as array
802
     *
803
     * @return array A collection of HttpPart objects
804
     */
805
    public function getParts()
806
    {
807
        return $this->parts;
808
    }
809
810
    /**
811
     * Adds a part to the parts collection.
812
     *
813
     * @param \AppserverIo\Appserver\ServletEngine\Http\Part $part A form part object
814
     * @param string                                         $name A manually defined name
815
     *
816
     * @return void
817
     */
818
    public function addPart(PartInterface $part, $name = null)
819
    {
820
        if ($name == null) {
821
            $name = $part->getName();
822
        }
823
        $this->parts[$name] = $part;
824
    }
825
826
    /**
827
     * Sets the application context name (application name prefixed with a slash) for the actual request.
828
     *
829
     * @param string $contextPath The application context name
830
     *
831
     * @return void
832
     */
833
    public function setContextPath($contextPath)
834
    {
835
        $this->contextPath = $contextPath;
836
    }
837
838
    /**
839
     * Returns the application context name (application name prefixed with a slash) for the actual request.
840
     *
841
     * @return string The application context name
842
     */
843
    public function getContextPath()
844
    {
845
        return $this->contextPath;
846
    }
847
848
    /**
849
     * Sets the path to the servlet used to handle this request.
850
     *
851
     * @param string $servletPath The path to the servlet
852
     *
853
     * @return void
854
     */
855
    public function setServletPath($servletPath)
856
    {
857
        $this->servletPath = $servletPath;
858
    }
859
860
    /**
861
     * Returns the path to the servlet used to handle this request.
862
     *
863
     * @return string The relative path to the servlet
864
     */
865
    public function getServletPath()
866
    {
867
        return $this->servletPath;
868
    }
869
870
    /**
871
     * Return the session identifier proposed by the actual configuration and request state.
872
     *
873
     * @return string The session identifier proposed for this request
874
     */
875
    public function getProposedSessionId()
876
    {
877
878
        // if no session has already been load, initialize the session manager
879
        /** @var \AppserverIo\Appserver\ServletEngine\SessionManagerInterface $manager */
880
        $manager = $this->getContext()->search(SessionManagerInterface::IDENTIFIER);
0 ignored issues
show
Bug introduced by
The method search() does not exist on AppserverIo\Psr\Context\ContextInterface. It seems like you code against a sub-type of AppserverIo\Psr\Context\ContextInterface such as AppserverIo\Psr\Naming\NamingDirectoryInterface or AppserverIo\Appserver\Application\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

880
        $manager = $this->getContext()->/** @scrutinizer ignore-call */ search(SessionManagerInterface::IDENTIFIER);
Loading history...
881
882
        // if no session manager was found, we don't support sessions
883
        if ($manager == null) {
884
            return;
885
        }
886
887
        // if we can't find a requested session name, we try to load the default session cookie
888
        if ($this->getRequestedSessionName() == null) {
889
            $this->setRequestedSessionName($manager->getSessionSettings()->getSessionName());
0 ignored issues
show
Bug introduced by
$manager->getSessionSettings()->getSessionName() of type string is incompatible with the type AppserverIo\Lang\String expected by parameter $requestedSessionName of AppserverIo\Appserver\Se...tRequestedSessionName(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

889
            $this->setRequestedSessionName(/** @scrutinizer ignore-type */ $manager->getSessionSettings()->getSessionName());
Loading history...
890
        }
891
892
        // load the requested session ID and name
893
        $sessionName = $this->getRequestedSessionName();
894
        $id = $this->getRequestedSessionId();
895
896
        // try to load session ID from session cookie of request/response
897
        $cookieFound = null;
898
        if ($id == null && $this->getResponse()->hasCookie($sessionName)) {
899
            $cookieFound = $this->getResponse()->getCookie($sessionName);
900
        } elseif ($id == null && $this->hasCookie($sessionName)) {
901
            $cookieFound = $this->getCookie($sessionName);
902
        }
903
904
        // check if we can find a cookie
905
        if (is_array($cookieFound)) {
0 ignored issues
show
introduced by
The condition is_array($cookieFound) is always false.
Loading history...
906
            // iterate over the cookies and try to find one that is not expired
907
            foreach ($cookieFound as $cookie) {
908
                if ($cookie instanceof CookieInterface && $cookie->isExpired() === false) {
909
                    $this->setRequestedSessionId($id = $cookie->getValue());
910
                }
911
            }
912
913
        // if we found a single cookie instance
914
        } elseif ($cookieFound instanceof CookieInterface && $cookieFound->isExpired() === false) {
915
            $this->setRequestedSessionId($id = $cookieFound->getValue());
916
        }
917
918
        // return the requested session
919
        return $id;
920
    }
921
922
    /**
923
     * Returns the session for this request.
924
     *
925
     * @param boolean $create TRUE to create a new session, else FALSE
926
     *
927
     * @return null|\AppserverIo\Psr\Servlet\Http\HttpSessionInterface The session instance
928
     *
929
     * @throws \Exception
930
     */
931
    public function getSession($create = false)
932
    {
933
934
        // load the proposed session-ID and name
935
        $id = $this->getProposedSessionId();
936
        $sessionName = $this->getRequestedSessionName();
937
938
        // if no session has already been load, initialize the session manager
939
        /** @var \AppserverIo\Appserver\ServletEngine\SessionManagerInterface $manager */
940
        $manager = $this->getSessionManager();
941
942
        // if no session manager was found, we don't support sessions
943
        if ($manager == null) {
944
            return;
945
        }
946
947
        // initialize empty session variable
948
        $session = null;
949
950
        // verify that the session id has a valid format
951
        if (preg_match('/^[A-z0-9]+$/', $id)) {
952
            // attempt to retrieve existing session
953
            $session = $manager->find($id);
954
        }
955
956
        // if we can't find a session or session has been expired and we want to create a new one
957
        if ($session == null && $create === true) {
958
            // always generate a unique identifier
959
            $id = SessionUtils::generateRandomString();
960
961
            // create a new session and register ID in request
962
            $session = $manager->create($id, $sessionName);
963
        }
964
965
        // if we can't find a session and we should NOT create one, return nothing
966
        if ($create === false && $session == null) {
967
            return;
968
        }
969
970
        // if we can't find a session although we SHOULD create one, we throw an exception
971
        if ($create === true && $session == null) {
972
            throw new \Exception('Can\'t create a new session!');
973
        }
974
975
        // initialize the session wrapper
976
        $wrapper = new SessionWrapper();
977
        $wrapper->injectSession($session);
0 ignored issues
show
Bug introduced by
It seems like $session can also be of type null; however, parameter $session of AppserverIo\Psr\Servlet\...rapper::injectSession() does only seem to accept AppserverIo\Psr\Servlet\ServletSessionInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

977
        $wrapper->injectSession(/** @scrutinizer ignore-type */ $session);
Loading history...
978
        $wrapper->injectRequest($this);
979
980
        // set the session ID in the execution environment
981
        Environment::singleton()->setAttribute(EnvironmentKeys::SESSION_ID, $id);
982
983
        // return the found session
984
        return $wrapper;
985
    }
986
987
    /**
988
     * Returns the absolute path info started from the context path.
989
     *
990
     * @return string The absolute path info
991
     */
992
    public function getPathInfo()
993
    {
994
        return $this->pathInfo;
995
    }
996
997
    /**
998
     * Sets the absolute path info started from the context path.
999
     *
1000
     * @param string $pathInfo The absolute path info
1001
     *
1002
     * @return void
1003
     */
1004
    public function setPathInfo($pathInfo)
1005
    {
1006
        $this->pathInfo = $pathInfo;
1007
    }
1008
1009
    /**
1010
     * Adds the passed cookie to this request.
1011
     *
1012
     * @param \AppserverIo\Psr\HttpMessage\CookieInterface $cookie The cookie to add
1013
     *
1014
     * @return void
1015
     */
1016
    public function addCookie(CookieInterface $cookie)
1017
    {
1018
        $this->getHttpRequest()->addCookie($cookie);
1019
    }
1020
1021
    /**
1022
     * Returns true if the request has a cookie header with the passed
1023
     * name, else false.
1024
     *
1025
     * @param string $cookieName Name of the cookie header to be checked
1026
     *
1027
     * @return boolean true if the request has the cookie, else false
1028
     */
1029
    public function hasCookie($cookieName)
1030
    {
1031
        return $this->getHttpRequest()->hasCookie($cookieName);
1032
    }
1033
1034
    /**
1035
     * Returns the value of the cookie with the passed name.
1036
     *
1037
     * @param string $cookieName The name of the cookie to return
1038
     *
1039
     * @return \AppserverIo\Psr\HttpMessage\CookieInterface The cookie instance
1040
     */
1041
    public function getCookie($cookieName)
1042
    {
1043
        return $this->getHttpRequest()->getCookie($cookieName);
1044
    }
1045
1046
    /**
1047
     * Return's the array with cookies.
1048
     *
1049
     * @return array The array with cookies
1050
     */
1051
    public function getCookies()
1052
    {
1053
        return $this->getHttpRequest()->getCookies();
0 ignored issues
show
Bug introduced by
The method getCookies() does not exist on AppserverIo\Psr\HttpMessage\RequestInterface. It seems like you code against a sub-type of AppserverIo\Psr\HttpMessage\RequestInterface such as AppserverIo\Http\HttpRequest or AppserverIo\Appserver\ServletEngine\Http\Request. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1053
        return $this->getHttpRequest()->/** @scrutinizer ignore-call */ getCookies();
Loading history...
1054
    }
1055
1056
    /**
1057
     * Returns header info by given name
1058
     *
1059
     * @param string $name The header key to name
1060
     *
1061
     * @return string|null
1062
     */
1063
    public function getHeader($name)
1064
    {
1065
        return $this->getHttpRequest()->getHeader($name);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getHttpRequest()->getHeader($name) returns the type string which is incompatible with the documented return type AppserverIo\Lang\String|null.
Loading history...
1066
    }
1067
1068
    /**
1069
     * Set headers data
1070
     *
1071
     * @param array $headers The headers array to set
1072
     *
1073
     * @return void
1074
     */
1075
    public function setHeaders(array $headers)
1076
    {
1077
        $this->getHttpRequest()->setHeaders($headers);
1078
    }
1079
1080
    /**
1081
     * Returns headers data
1082
     *
1083
     * @return array
1084
     */
1085
    public function getHeaders()
1086
    {
1087
        return $this->getHttpRequest()->getHeaders();
1088
    }
1089
1090
    /**
1091
     * Adds a header information got from connection.
1092
     *
1093
     * @param string $name  The header name
1094
     * @param string $value The headers value
1095
     *
1096
     * @return void
1097
     */
1098
    public function addHeader($name, $value)
1099
    {
1100
        $this->getHttpRequest()->addHeader($name, $value);
1101
    }
1102
1103
    /**
1104
     * Checks if header exists by given name.
1105
     *
1106
     * @param string $name The header name to check
1107
     *
1108
     * @return boolean
1109
    */
1110
    public function hasHeader($name)
1111
    {
1112
        return $this->getHttpRequest()->hasHeader($name);
1113
    }
1114
1115
    /**
1116
     * Set the requested session ID for this request.  This is normally called
1117
     * by the HTTP Connector, when it parses the request headers.
1118
     *
1119
     * @param string $requestedSessionId The new session id
1120
     *
1121
     * @return void
1122
     */
1123
    public function setRequestedSessionId($requestedSessionId)
1124
    {
1125
        $this->requestedSessionId = $requestedSessionId;
1126
    }
1127
1128
    /**
1129
     * Return the session identifier included in this request, if any.
1130
     *
1131
     * @return string The session identifier included in this request
1132
     */
1133
    public function getRequestedSessionId()
1134
    {
1135
        return $this->requestedSessionId;
1136
    }
1137
1138
    /**
1139
     * Set the requested session name for this request.
1140
     *
1141
     * @param string $requestedSessionName The new session name
1142
     *
1143
     * @return void
1144
     */
1145
    public function setRequestedSessionName($requestedSessionName)
1146
    {
1147
        $this->requestedSessionName = $requestedSessionName;
1148
    }
1149
1150
    /**
1151
     * Return the session name included in this request, if any.
1152
     *
1153
     * @return string The session name included in this request
1154
     */
1155
    public function getRequestedSessionName()
1156
    {
1157
        return $this->requestedSessionName;
1158
    }
1159
1160
    /**
1161
     * Sets the flag to mark the request dispatched.
1162
     *
1163
     * @param boolean $dispatched TRUE if the request has already been dispatched, else FALSE
1164
     *
1165
     * @return void
1166
     */
1167
    public function setDispatched($dispatched = true)
1168
    {
1169
        $this->dispatched = $dispatched;
1170
    }
1171
1172
    /**
1173
     * Sets the flag that shows if the request has already been dispatched.
1174
     *
1175
     * @return boolean TRUE if the request has already been dispatched, else FALSE
1176
     */
1177
    public function isDispatched()
1178
    {
1179
        return $this->dispatched;
1180
    }
1181
1182
    /**
1183
     * Sets the server name.
1184
     *
1185
     * @param string $serverName The server name
1186
     *
1187
     * @return void
1188
     */
1189
    public function setServerName($serverName)
1190
    {
1191
        $this->serverName = $serverName;
1192
    }
1193
1194
    /**
1195
     * Returns the server name.
1196
     *
1197
     * @return string The server name
1198
     */
1199
    public function getServerName()
1200
    {
1201
        return $this->serverName;
1202
    }
1203
1204
    /**
1205
     * Sets the query string of the actual request.
1206
     *
1207
     * @param string $queryString The query string
1208
     *
1209
     * @return void
1210
     */
1211
    public function setQueryString($queryString)
1212
    {
1213
        $this->queryString = $queryString;
1214
    }
1215
1216
    /**
1217
     * Returns query string of the actual request.
1218
     *
1219
     * @return string|null The query string of the actual request
1220
     */
1221
    public function getQueryString()
1222
    {
1223
        return $this->queryString;
1224
    }
1225
1226
    /**
1227
     * Returns request uri
1228
     *
1229
     * @return string
1230
     */
1231
    public function getUri()
1232
    {
1233
        return $this->getHttpRequest()->getUri();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getHttpRequest()->getUri() returns the type string which is incompatible with the documented return type AppserverIo\Lang\String.
Loading history...
1234
    }
1235
1236
    /**
1237
     * Sets the URI.
1238
     *
1239
     * @param string $uri The uri
1240
     *
1241
     * @return void
1242
     */
1243
    public function setUri($uri)
1244
    {
1245
        $this->getHttpRequest()->setUri($uri);
1246
    }
1247
1248
    /**
1249
     * Returns request method
1250
     *
1251
     * @return string
1252
     */
1253
    public function getMethod()
1254
    {
1255
        return $this->getHttpRequest()->getMethod();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getHttpRequest()->getMethod() returns the type string which is incompatible with the documented return type AppserverIo\Lang\String.
Loading history...
1256
    }
1257
1258
    /**
1259
     * Sets the method to be performed on the resource identified by the
1260
     * Request-URI.
1261
     *
1262
     * While HTTP method names are typically all uppercase characters, HTTP
1263
     * method names are case-sensitive and thus implementations SHOULD NOT
1264
     * modify the given string.
1265
     *
1266
     * @param string $method Case-insensitive method
1267
     *
1268
     * @return void
1269
     */
1270
    public function setMethod($method)
1271
    {
1272
        $this->getHttpRequest()->setMethod($method);
1273
    }
1274
1275
    /**
1276
     * Returns the array with the server variables.
1277
     *
1278
     * @return array The array with the server variables
1279
     */
1280
    public function getServerVars()
1281
    {
1282
        return $this->serverVars;
1283
    }
1284
1285
    /**
1286
     * Returns the server variable with the requested name.
1287
     *
1288
     * @param string $name The name of the server variable to be returned
1289
     *
1290
     * @return mixed The requested server variable
1291
     */
1292
    public function getServerVar($name)
1293
    {
1294
        if (isset($this->serverVars[$name])) {
1295
            return $this->serverVars[$name];
1296
        }
1297
    }
1298
1299
    /**
1300
     * Set's the passed authentication type.
1301
     *
1302
     * @param string|null $authType The authentication type
1303
     *
1304
     * @return void
1305
     */
1306
    public function setAuthType($authType = null)
1307
    {
1308
        $this->authType = $authType;
1309
    }
1310
1311
    /**
1312
     * Return's the authentication type.
1313
     *
1314
     * @return string|null The authentication type
1315
     */
1316
    public function getAuthType()
1317
    {
1318
        return $this->authType;
1319
    }
1320
1321
    /**
1322
     * Set's the user principal for this request.
1323
     *
1324
     * @param \AppserverIo\Psr\Security\PrincipalInterface|null $userPrincipal The user principal
1325
     *
1326
     * @return void
1327
     */
1328
    public function setUserPrincipal(PrincipalInterface $userPrincipal = null)
1329
    {
1330
        $this->userPrincipal = $userPrincipal;
1331
    }
1332
1333
    /**
1334
     * Return's a PrincipalInterface object containing the name of the current authenticated user.
1335
     *
1336
     * @return \AppserverIo\Psr\Security\PrincipalInterface|null The user principal
1337
     */
1338
    public function getUserPrincipal()
1339
    {
1340
        return $this->userPrincipal;
1341
    }
1342
1343
    /**
1344
     * Return's the login of the user making this request, if the user has been authenticated, or null if the
1345
     * user has not been authenticated. Whether the user name is sent with each subsequent request depends on
1346
     * the browser and type of authentication. Same as the value of the CGI variable REMOTE_USER.
1347
     *
1348
     * @return \AppserverIo\Lang\String|null A string specifying the login of the user making this request, or null if the user login is not known
1349
     */
1350
    public function getRemoteUser()
1351
    {
1352
        if ($userPrincipal = $this->getUserPrincipal()) {
1353
            return $userPrincipal->getName();
1354
        }
1355
    }
1356
1357
    /**
1358
     * Return_s a boolean indicating whether the authenticated user is included in the specified logical "role".
1359
     *
1360
     * @param \AppserverIo\Lang\String $role The role we want to test for
1361
     *
1362
     * @return boolean TRUE if the user has the passed role, else FALSE
1363
     */
1364
    public function isUserInRole(String $role)
1365
    {
1366
1367
        // query whether or not we've an user principal
1368
        if ($principal = $this->getUserPrincipal()) {
1369
            return $principal->getRoles()->contains($role);
0 ignored issues
show
Bug introduced by
The method getRoles() does not exist on AppserverIo\Psr\Security\PrincipalInterface. It seems like you code against a sub-type of AppserverIo\Psr\Security\PrincipalInterface such as AppserverIo\Appserver\Se...curity\GenericPrincipal. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1369
            return $principal->/** @scrutinizer ignore-call */ getRoles()->contains($role);
Loading history...
1370
        }
1371
1372
        // user is not in passed role
1373
        return false;
1374
    }
1375
1376
    /**
1377
     * Use the container login mechanism configured for the servlet context to authenticate the user making this
1378
     * request. This method may modify and commit the passed servlet response.
1379
     *
1380
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The servlet response
1381
     *
1382
     * @return boolean TRUE when non-null values were or have been established as the values returned by getRemoteUser, else FALSE
1383
     */
1384
    public function authenticate(HttpServletResponseInterface $servletResponse)
1385
    {
1386
1387
        // load the authentication manager and try to authenticate this request
1388
        /** @var \AppserverIo\Psr\Auth\AuthenticationManagerInterface $authenticationManager */
1389
        if ($authenticationManager = $this->getAuthenticationManager()) {
1390
            return $authenticationManager->handleRequest($this, $servletResponse);
1391
        }
1392
1393
        // also return TRUE if we can't find an authentication manager
1394
        return true;
1395
    }
1396
1397
    /**
1398
     * Validate the provided username and password in the password validation realm used by the web
1399
     * container login mechanism configured for the ServletContext.
1400
     *
1401
     * @param \AppserverIo\Lang\String $username The username to login
1402
     * @param \AppserverIo\Lang\String $password The password used to authenticate the user
1403
     *
1404
     * @return void
1405
     * @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if no default authenticator can be found
1406
     */
1407
    public function login(String $username, String $password)
1408
    {
1409
1410
        // query whether or not we're already authenticated or not
1411
        if ($this->getAuthType() != null || $this->getRemoteUser() != null || $this->getUserPrincipal() != null) {
1412
            throw new ServletException('Already authenticated');
1413
        }
1414
1415
        // load the authentication manager and try to authenticate this request
1416
        /** @var \AppserverIo\Psr\Auth\AuthenticationManagerInterface $authenticationManager */
1417
        if ($authenticationManager = $this->getAuthenticationManager()) {
1418
            // try to load the authentication managers default authenticator
1419
            if (($authenticator = $authenticationManager->getAuthenticator()) == null) {
1420
                throw new ServletException('Can\'t find default authenticator');
1421
            }
1422
1423
            // authenticate the passed username/password combination
1424
            $authenticator->login($username, $password, $this);
0 ignored issues
show
Bug introduced by
The method login() does not exist on AppserverIo\Storage\StorageInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1424
            $authenticator->/** @scrutinizer ignore-call */ 
1425
                            login($username, $password, $this);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1425
        }
1426
    }
1427
1428
    /**
1429
     * Establish null as the value returned when getUserPrincipal, getRemoteUser, and getAuthType is
1430
     * called on the request.
1431
     *
1432
     * @return void
1433
     * @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if no default authenticator can be found
1434
     */
1435
    public function logout()
1436
    {
1437
1438
        // load the authentication manager and try to authenticate this request
1439
        /** @var \AppserverIo\Psr\Auth\AuthenticationManagerInterface $authenticationManager */
1440
        if ($authenticationManager = $this->getAuthenticationManager()) {
1441
            // try to load the authentication managers default authenticator
1442
            if (($authenticator = $authenticationManager->getAuthenticator()) == null) {
1443
                throw new ServletException('Can\'t find default authenticator');
1444
            }
1445
1446
            // logout the actual user
1447
            $authenticator->logout($this);
0 ignored issues
show
Bug introduced by
The method logout() does not exist on AppserverIo\Storage\StorageInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1447
            $authenticator->/** @scrutinizer ignore-call */ 
1448
                            logout($this);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1448
        }
1449
    }
1450
}
1451