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

OCSMiddleware   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 107
Duplicated Lines 3.74 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 4
loc 107
rs 10
wmc 22
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
C afterException() 0 41 13
A getFormat() 4 12 2
B afterController() 0 22 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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