@@ -70,26 +70,40 @@ public IcatClient(String url, String sessionId) {
7070 * @param password ICAT password
7171 * @return json with sessionId of the form
7272 * <samp>{"sessionId","0d9a3706-80d4-4d29-9ff3-4d65d4308a24"}</samp>
73- * @throws BadRequestException
73+ * @throws TopcatException
7474 */
75- public String login (String plugin , String username , String password ) throws BadRequestException {
75+ public String login (String plugin , String username , String password ) throws TopcatException {
76+ JsonObjectBuilder objectBuilder = Json .createObjectBuilder ();
77+ JsonArrayBuilder arrayBuilder = Json .createArrayBuilder ();
78+ JsonObjectBuilder usernameBuilder = Json .createObjectBuilder ();
79+ JsonObjectBuilder passwordBuilder = Json .createObjectBuilder ();
80+ usernameBuilder .add ("username" , username );
81+ passwordBuilder .add ("password" , password );
82+ arrayBuilder .add (usernameBuilder );
83+ arrayBuilder .add (passwordBuilder );
84+ objectBuilder .add ("plugin" , plugin );
85+ objectBuilder .add ("credentials" , arrayBuilder );
86+ String jsonString = "json=" + objectBuilder .build ().toString ();
87+ Response response ;
7688 try {
77- JsonObjectBuilder objectBuilder = Json .createObjectBuilder ();
78- JsonArrayBuilder arrayBuilder = Json .createArrayBuilder ();
79- JsonObjectBuilder usernameBuilder = Json .createObjectBuilder ();
80- JsonObjectBuilder passwordBuilder = Json .createObjectBuilder ();
81- usernameBuilder .add ("username" , username );
82- passwordBuilder .add ("password" , password );
83- arrayBuilder .add (usernameBuilder );
84- arrayBuilder .add (passwordBuilder );
85- objectBuilder .add ("plugin" , plugin );
86- objectBuilder .add ("credentials" , arrayBuilder );
87- String jsonString = "json=" + objectBuilder .build ().toString ();
88- Response response = httpClient .post ("session" , new HashMap <String , String >(), jsonString );
89- return response .toString ();
89+ response = httpClient .post ("session" , new HashMap <String , String >(), jsonString );
9090 } catch (Exception e ) {
9191 throw new BadRequestException (e .getMessage ());
9292 }
93+ switch (response .getCode ()) {
94+ case 200 :
95+ return response .toString ();
96+ case 400 :
97+ throw new BadRequestException (response .toString ());
98+ case 401 :
99+ throw new AuthenticationException (response .toString ());
100+ case 403 :
101+ throw new ForbiddenException (response .toString ());
102+ case 404 :
103+ throw new NotFoundException (response .toString ());
104+ default :
105+ throw new InternalException (response .toString ());
106+ }
93107 }
94108
95109 public String getUserName () throws TopcatException {
0 commit comments