5
5
import fr .rabian .ovhApi .core .http .Header ;
6
6
import fr .rabian .ovhApi .core .http .HttpRequests ;
7
7
import fr .rabian .ovhApi .core .utils .HashFunctions ;
8
+ import fr .rabian .ovhApi .core .utils .NOKResponseException ;
8
9
import fr .rabian .ovhApi .core .utils .Timestamps ;
9
10
11
+ import java .security .NoSuchAlgorithmException ;
10
12
import java .util .ArrayList ;
11
13
import java .util .List ;
12
14
@@ -50,8 +52,9 @@ protected AppManager(Application app) {
50
52
* @param scope Authorization scope
51
53
* @param redirectURL URL to redirect to after authentication
52
54
* @return Feteched Consumer
55
+ * @throws NOKResponseException If the response differs from 200 - OK
53
56
*/
54
- public Consumer getConsumer (List <ScopeElement > scope , String redirectURL ) {
57
+ public Consumer getConsumer (List <ScopeElement > scope , String redirectURL ) throws NOKResponseException {
55
58
GsonBuilder builder = new GsonBuilder ();
56
59
Gson gson = builder .create ();
57
60
List <Header > headers = new ArrayList <>();
@@ -61,16 +64,13 @@ public Consumer getConsumer(List<ScopeElement> scope, String redirectURL) {
61
64
62
65
RequestCredentials rc = new RequestCredentials (scope , redirectURL );
63
66
String req = gson .toJson (rc );
64
- int result = 0 ;
65
- try {
66
- result = HttpRequests .sendPost (ep .getURL () + "/auth/credential" , out , req , headers );
67
- } catch (Exception e ) {
68
- e .printStackTrace ();
69
- }
70
- Consumer c = null ;
67
+ int result = HttpRequests .sendPost (ep .getURL () + "/auth/credential" , out , req , headers );
68
+ Consumer c ;
71
69
if (result == 200 ) {
72
70
c = gson .fromJson (out .toString (), Consumer .class );
73
71
c .setScope (scope );
72
+ } else {
73
+ throw new NOKResponseException (result );
74
74
}
75
75
return c ;
76
76
}
@@ -81,9 +81,9 @@ public Consumer getConsumer(List<ScopeElement> scope, String redirectURL) {
81
81
* @param path Path to the function
82
82
* @param c Consumer concerned
83
83
* @return Response body
84
- * @throws Exception Occuring during the request
84
+ * @throws NOKResponseException If the response differs from 200 - OK
85
85
*/
86
- public String sendGetReq (String path , Consumer c ) throws Exception {
86
+ public String sendGetReq (String path , Consumer c ) throws NOKResponseException {
87
87
return sendReq (path , "GET" , c , "" );
88
88
}
89
89
@@ -93,9 +93,9 @@ public String sendGetReq(String path, Consumer c) throws Exception {
93
93
* @param path Path to the function
94
94
* @param c Consumer concerned
95
95
* @return Response body
96
- * @throws Exception Occuring during the request
96
+ * @throws NOKResponseException If the response differs from 200 - OK
97
97
*/
98
- public String sendDeleteReq (String path , Consumer c ) throws Exception {
98
+ public String sendDeleteReq (String path , Consumer c ) throws NOKResponseException {
99
99
return sendReq (path , "DELETE" , c , "" );
100
100
}
101
101
@@ -106,9 +106,9 @@ public String sendDeleteReq(String path, Consumer c) throws Exception {
106
106
* @param c Consumer concerned
107
107
* @param body Request body
108
108
* @return Response body
109
- * @throws Exception Occuring during the request
109
+ * @throws NOKResponseException If the response differs from 200 - OK
110
110
*/
111
- public String sendPostReq (String path , Consumer c , String body ) throws Exception {
111
+ public String sendPostReq (String path , Consumer c , String body ) throws NOKResponseException {
112
112
return sendReq (path , "POST" , c , body );
113
113
}
114
114
@@ -119,9 +119,9 @@ public String sendPostReq(String path, Consumer c, String body) throws Exception
119
119
* @param c Consumer concerned
120
120
* @param body Request body
121
121
* @return Response body
122
- * @throws Exception Occuring during the request
122
+ * @throws NOKResponseException If the response differs from 200 - OK
123
123
*/
124
- public String sendPutReq (String path , Consumer c , String body ) throws Exception {
124
+ public String sendPutReq (String path , Consumer c , String body ) throws NOKResponseException {
125
125
return sendReq (path , "PUT" , c , body );
126
126
}
127
127
@@ -133,9 +133,9 @@ public String sendPutReq(String path, Consumer c, String body) throws Exception
133
133
* @param c Consumer concerned
134
134
* @param body Request body
135
135
* @return Response body
136
- * @throws Exception Occuring during the request
136
+ * @throws NOKResponseException If the response differs from 200 - OK
137
137
*/
138
- private String sendReq (String path , String method , Consumer c , String body ) throws Exception {
138
+ private String sendReq (String path , String method , Consumer c , String body ) throws NOKResponseException {
139
139
List <Header > headers = new ArrayList <>();
140
140
path = ep .getURL () + path ;
141
141
long time = ts .getTime ();
@@ -151,15 +151,21 @@ private String sendReq(String path, String method, Consumer c, String body) thro
151
151
forSig .append (body );
152
152
forSig .append ("+" );
153
153
forSig .append (Long .toString (time ));
154
- String sig = "$1$" + HashFunctions .hashMD ("SHA-1" , forSig .toString ());
154
+
155
+ String sig = "$1$" ;
156
+ try {
157
+ sig = sig + HashFunctions .hashMD ("SHA-1" , forSig .toString ());
158
+ } catch (NoSuchAlgorithmException e ) {
159
+ //Silenced because always available.
160
+ }
155
161
156
162
headers .add (new Header ("X-Ovh-Application" , app .getPubKey ()));
157
163
headers .add (new Header ("X-Ovh-Consumer" , c .getConsumerKey ()));
158
164
headers .add (new Header ("Accept" , "application/json" ));
159
165
headers .add (new Header ("X-Ovh-Timestamp" , Long .toString (time )));
160
166
headers .add (new Header ("X-Ovh-Signature" , sig ));
161
167
162
- int result = 0 ;
168
+ int result ;
163
169
StringBuffer out = new StringBuffer ();
164
170
165
171
switch (method ) {
@@ -179,6 +185,10 @@ private String sendReq(String path, String method, Consumer c, String body) thro
179
185
throw new IllegalArgumentException ("Incorrect HTTP method." );
180
186
}
181
187
182
- return out .toString ();
188
+ if (result == 200 ) {
189
+ return out .toString ();
190
+ } else {
191
+ throw new NOKResponseException (result );
192
+ }
183
193
}
184
194
}
0 commit comments