View Javadoc
1   package com.srv4pos.server.api.auth;
2   
3   import com.srv4pos.server.api.infrastructure.EmptyConnectionWriter;
4   import com.srv4pos.server.api.infrastructure.HttpMethod;
5   import com.srv4pos.server.api.infrastructure.credentials.Credentials;
6   import com.srv4pos.server.api.infrastructure.exceptions.enums.ForbiddenErrorType;
7   import com.srv4pos.server.api.infrastructure.http.HttpHelper;
8   import com.srv4pos.server.api.infrastructure.http.Transport;
9   
10  import java.net.URL;
11  
12  import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestString;
13  
14  /**
15   * Common aggregation for utility methods requires authentication. User: Kirill Date: 25.07.13 11:14
16   */
17  public class AuthService {
18      private URL url;
19      private Credentials credentials;
20      private Transport transport;
21  
22      public AuthService() {
23  
24      }
25  
26      public AuthService(URL url, Credentials credentials, Transport transport) {
27          this.url = url;
28          this.credentials = credentials;
29          this.transport = transport;
30      }
31  
32      /**
33       * This method is very useful to check authentication. Call it to make sure you are successfully authenticated.
34       *
35       * @return {@code HandshakeReply} indicates whether request version is outdated.
36       * @throws com.srv4pos.server.api.exceptions.ForbiddenJsonException see: {@link ForbiddenErrorType#SELLER_NOT_FOUND}
37       */
38      public HandshakeReply handshake() {
39          return HandshakeReply.fromJsonToHandshakeReply(
40                  requestString("auth/handshake", HttpMethod.GET, url, credentials == null ? null : credentials.toAuthentication(), null, null,
41                          EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
42                          HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport).getContent());
43      }
44  
45      /**
46       * Gets user info and list with connected sellers to the user in credentials.
47       *
48       * @return instance of {@link com.srv4pos.server.api.auth.AuthInfo}
49       * @throws com.srv4pos.server.api.exceptions.ForbiddenJsonException see:
50       *                                                                  {@link ForbiddenErrorType#INSUFFICIENT_ACCESS_RIGHTS}
51       * @see AuthServiceSample
52       */
53      public AuthInfo getAuth() {
54          return AuthInfo.fromJsonToAuthInfo(requestString(String.format("auth/user"), HttpMethod.GET, url, credentials.toAuthentication(), null, null,
55                  EmptyConnectionWriter.EMPTY_CONNECTION_WRITER, HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport).getContent());
56      }
57  }