View Javadoc
1   package com.srv4pos.server.api.posplus;
2   
3   import com.srv4pos.server.api.infrastructure.HttpMethod;
4   import com.srv4pos.server.api.infrastructure.StringConnectionWriter;
5   import com.srv4pos.server.api.infrastructure.exceptions.enums.InconsistentDataErrorType;
6   import com.srv4pos.server.api.infrastructure.credentials.Credentials;
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  import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestVoid;
14  import static java.lang.String.format;
15  
16  /**
17   * For sending data to remote control unit.
18   * <p>User: Kirill, Date: 08.03.14 17:12</p>
19   */
20  public class PosPlusService {
21      private URL url;
22      private Credentials credentials;
23      private Transport transport;
24  
25      /**
26       * Constructs the service.
27       *
28       * @param url         of the server
29       * @param credentials to work with the server
30       * @param transport   to know the method to reach the server
31       */
32      public PosPlusService(URL url, Credentials credentials, Transport transport) {
33          this.url = url;
34          this.credentials = credentials;
35          this.transport = transport;
36      }
37  
38  /*
39      public Collection<PosPlusInfo> posPluses() {
40          return PosPlusInfo.fromJsonArrayToPosPlusInfoes(
41                  request(
42                          "pospluses",
43                          HttpMethod.GET,
44                          url,
45                          credentials,
46                          null,
47                          null,
48                          EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
49                          ServiceHelper.CONTENT_TYPE_APPLICATION_JSON,
50                          transport).toText());
51      }
52  
53      public PosPlusInfo updateAndGetPosPlusInfo(String id) {
54          return PosPlusInfo.fromJsonToPosPlusInfo(
55                  request(
56                          String.format("posplus/%s", id),
57                          HttpMethod.GET,
58                          url,
59                          credentials,
60                          null,
61                          null,
62                          EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
63                          ServiceHelper.CONTENT_TYPE_APPLICATION_JSON,
64                          transport).toText());
65      }
66  */
67  
68      /**
69       * Sends data to control unit.
70       *
71       * @param kdInfo data to send
72       * @return result of sending
73       * @throws com.srv4pos.server.api.exceptions.ControlUnitReceiptJsonException       when Control Unit decides something is
74       *                                                                                 wrong according to it's internal business logic
75       * @throws com.srv4pos.server.api.exceptions.InconsistentDataJsonException         see:
76       *                                                                                 {@link InconsistentDataErrorType#WORKING_WITH_CU_IS_NOT_SUPPORTED_BY_APP}
77       * @throws com.srv4pos.server.api.exceptions.ControlUnitUnavailableJsonException   when server is unable to reach the control
78       *                                                                                 unit device (for instance it's ejected from the server)
79       * @throws com.srv4pos.server.api.exceptions.ControlUnitCommunicationJsonException when error has happened during transmission data to the control unit
80       *                                                                                 (for instance, it was ejected from the server during sending the data)
81       *                                                                                 workable yet
82       */
83      public KdReplyInfo kd(KdInfo kdInfo) {
84          return KdReplyInfo.fromJsonToKdReplyInfo(
85                  requestString(
86                          "pospluses/kd",
87                          HttpMethod.POST,
88                          url,
89                          credentials.toAuthentication(),
90                          null,
91                          null,
92                          new StringConnectionWriter(kdInfo.toJson()),
93                          HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
94                          transport).getContent());
95      }
96  
97      /**
98       * The same as {@link #kd(KdInfo)}, but also has param logId for load testing.
99       * For logging details, see com.srv4pos.server.core.web.infrastructure.MethodProfiler
100      * and com.srv4pos.server.core.web.infrastructure.ServletTimeMeasurementFilter.
101      *
102      * @param kdInfo data to send
103      * @param logId  the identifier of log
104      * @return result of sending
105      */
106     public KdReplyInfo kd(KdInfo kdInfo, String logId) {
107         return KdReplyInfo.fromJsonToKdReplyInfo(
108                 requestString(
109                         format("pospluses/kd?logId=%s", logId),
110                         HttpMethod.POST,
111                         url,
112                         credentials.toAuthentication(),
113                         null,
114                         null,
115                         new StringConnectionWriter(kdInfo.toJson()),
116                         HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
117                         transport).getContent());
118     }
119 
120     /**
121      * Creates new entity.
122      *
123      * @param controlUnitEndpointInfo contains information about new control unit endpoint
124      */
125     public void create(ControlUnitEndpointInfo controlUnitEndpointInfo) {
126         requestVoid("control-unit-endpoints",
127                 HttpMethod.POST,
128                 url,
129                 credentials.toAuthentication(),
130                 null,
131                 null,
132                 new StringConnectionWriter(controlUnitEndpointInfo.toJson()),
133                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
134                 transport);
135     }
136 }