Completed
Push — master ( c1632c...027069 )
by Joas
22:03 queued 08:37
created

OCSMiddleware::afterException()   C

Complexity

Conditions 13
Paths 29

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 23
c 1
b 0
f 0
nc 29
nop 3
dl 0
loc 41
rs 5.1234

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OC\AppFramework\Middleware;
24
25
use OC\AppFramework\Http;
26
use OCP\API;
27
use OCP\AppFramework\Http\DataResponse;
28
use OCP\AppFramework\Http\JSONResponse;
29
use OCP\AppFramework\Http\OCSResponse;
30
use OCP\AppFramework\Http\Response;
31
use OCP\AppFramework\OCS\OCSException;
32
use OCP\AppFramework\OCS\OCSForbiddenException;
33
use OCP\AppFramework\OCS\OCSNotFoundException;
34
use OCP\AppFramework\OCSController;
35
use OCP\IRequest;
36
use OCP\AppFramework\Middleware;
37
38
class OCSMiddleware extends Middleware {
39
40
	/** @var IRequest */
41
	private $request;
42
43
	/**
44
	 * @param IRequest $request
45
	 */
46
	public function __construct(IRequest $request) {
47
		$this->request = $request;
48
	}
49
50
	/**
51
	 * @param \OCP\AppFramework\Controller $controller
52
	 * @param string $methodName
53
	 * @param \Exception $exception
54
	 * @throws \Exception
55
	 * @return OCSResponse
56
	 */
57
	public function afterException($controller, $methodName, \Exception $exception) {
58
		if ($controller instanceof OCSController && $exception instanceof OCSException) {
59
			$format = $this->getFormat($controller);
60
61
			$code = $exception->getCode();
62
			if ($code === 0) {
63
				$code = API::RESPOND_UNKNOWN_ERROR;
64
			}
65
66
			// Build the response
67
			$response = new OCSResponse($format, $code, $exception->getMessage());
68
69
			// Forbidden always sets 401 (even on v1.php)
70
			if ($exception instanceof OCSForbiddenException || $code === API::RESPOND_UNAUTHORISED) {
71
				$response->setStatus(Http::STATUS_UNAUTHORIZED);
72
			}
73
74
			// On v2.php we set actual HTTP error codes
75
			if (substr_compare($this->request->getScriptName(), '/ocs/v2.php', -strlen('/ocs/v2.php')) === 0) {
76
				if ($code === API::RESPOND_NOT_FOUND) {
77
					$response->setStatus(Http::STATUS_NOT_FOUND);
78
				} else if ($code === API::RESPOND_SERVER_ERROR) {
79
					$response->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR);
80
				} else if ($code === API::RESPOND_UNKNOWN_ERROR) {
81
					$response->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR);
82
				} else if ($code === API::RESPOND_UNAUTHORISED) {
83
					// Already set
84
				}
85
				// 4xx and 5xx codes are forwarded as is.
86
				else if ($code >= 400 && $code < 600) {
87
					$response->setStatus($code);
88
				} else {
89
					// All other codes get a bad request
90
					$response->setStatus(Http::STATUS_BAD_REQUEST);
91
				}
92
			}
93
			return $response;
94
		}
95
96
		throw $exception;
97
	}
98
99
	/**
100
	 * @param \OCP\AppFramework\Controller $controller
101
	 * @param string $methodName
102
	 * @param Response $response
103
	 * @return \OCP\AppFramework\Http\Response
104
	 */
105
	public function afterController($controller, $methodName, Response $response) {
106
		/*
107
		 * If a different middleware has detected that a request unauthorized or forbidden
108
		 * we need to catch the response and convert it to a proper OCS response.
109
		 */
110
		if ($controller instanceof OCSController && !($response instanceof OCSResponse)) {
111
			if ($response->getStatus() === Http::STATUS_UNAUTHORIZED ||
112
			    $response->getStatus() === Http::STATUS_FORBIDDEN) {
113
				$format = $this->getFormat($controller);
114
115
				$message = '';
116
				if ($response instanceof JSONResponse) {
117
					/** @var DataResponse $response */
118
					$message = $response->getData()['message'];
119
				}
120
				$response = new OCSResponse($format, \OCP\API::RESPOND_UNAUTHORISED, $message);
121
				$response->setStatus(Http::STATUS_UNAUTHORIZED);
122
			}
123
		}
124
125
		return $response;
126
	}
127
128
	/**
129
	 * @param \OCP\AppFramework\Controller $controller
130
	 * @return string
131
	 */
132
	private function getFormat($controller) {
133
		// get format from the url format or request format parameter
134
		$format = $this->request->getParam('format');
135
136
		// if none is given try the first Accept header
137 View Code Duplication
		if($format === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
			$headers = $this->request->getHeader('Accept');
139
			$format = $controller->getResponderByHTTPHeader($headers, 'xml');
140
		}
141
142
		return $format;
143
	}
144
}
145