View Javadoc
1   package emissary.server.mvc.adapters;
2   
3   import emissary.client.EmissaryClient;
4   import emissary.client.EmissaryResponse;
5   import emissary.directory.KeyManipulator;
6   import emissary.pickup.WorkBundle;
7   
8   import org.apache.hc.client5.http.classic.methods.HttpPost;
9   import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
10  import org.apache.hc.core5.http.HttpStatus;
11  import org.apache.hc.core5.http.NameValuePair;
12  import org.apache.hc.core5.http.message.BasicNameValuePair;
13  import org.slf4j.Logger;
14  import org.slf4j.LoggerFactory;
15  
16  import java.nio.charset.StandardCharsets;
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  /**
21   * Stuff for adapting the WorkSpace remote call to HTTP
22   */
23  public class WorkSpaceAdapter extends EmissaryClient {
24  
25      private static final Logger logger = LoggerFactory.getLogger(WorkSpaceAdapter.class);
26  
27      public static final String CLIENT_NAME = "pickUpClientName";
28      public static final String SPACE_NAME = "workSpaceName";
29      public static final String WORK_BUNDLE_ID = "tpId";
30      public static final String WORK_BUNDLE_STATUS = "tpStatus";
31  
32      /**
33       * Outbound open tells a remote WorkSpace to start pulling data
34       * 
35       * @param place the remote place to contact
36       * @param space the location of the work distributor
37       */
38      public EmissaryResponse outboundOpenWorkSpace(final String place, final String space) {
39  
40          final String placeUrl = KeyManipulator.getServiceHostUrl(place);
41          final HttpPost method = createHttpPost(placeUrl, context, "/WorkSpaceClientOpenWorkSpace.action");
42  
43          final List<NameValuePair> nvps = new ArrayList<>();
44          nvps.add(new BasicNameValuePair(CLIENT_NAME, place));
45          nvps.add(new BasicNameValuePair(SPACE_NAME, space));
46          method.setEntity(new UrlEncodedFormEntity(nvps, StandardCharsets.UTF_8));
47  
48          // {@link EmissaryClient#client} is configured with connection and socket timeout
49  
50          return send(method);
51      }
52  
53      /**
54       * Outbound take grabs a WorkBundle from remote WorkSpace
55       * 
56       * @param space the remote space to contact
57       * @param place the name of the requesting place
58       */
59      public WorkBundle outboundWorkSpaceTake(final String space, final String place) {
60  
61          final String placeUrl = KeyManipulator.getServiceHostUrl(space);
62          final HttpPost method = createHttpPost(placeUrl, context, "/WorkSpaceClientSpaceTake.action");
63  
64          final List<NameValuePair> nvps = new ArrayList<>();
65          nvps.add(new BasicNameValuePair(CLIENT_NAME, place));
66          nvps.add(new BasicNameValuePair(SPACE_NAME, space));
67  
68          method.setEntity(new UrlEncodedFormEntity(nvps, StandardCharsets.UTF_8));
69          final EmissaryResponse status = send(method);
70  
71          WorkBundle path = null;
72          // TODO Look at putting this method in the EmissaryResponse
73          if (status.getStatus() != HttpStatus.SC_OK) {
74              logger.debug("Take from space {} was an error: {}", space, status.getContentString());
75          } else {
76              path = WorkBundle.buildWorkBundle(status.getContentString());
77          }
78          return path;
79      }
80  
81      /**
82       * Outbound notice that bundle was completed
83       * 
84       * @param space the remote space to contact
85       * @param place the name of the notifying place
86       * @param bundleId the id of the bundle that was completed
87       * @param itWorked status of the processing
88       * @return true if the message was sent
89       */
90      public boolean outboundBundleCompletion(final String space, final String place, final String bundleId, final boolean itWorked) {
91          final String placeUrl = KeyManipulator.getServiceHostUrl(space);
92          final HttpPost method = createHttpPost(placeUrl, context, "/WorkBundleCompleted.action");
93  
94          final List<NameValuePair> nvps = new ArrayList<>();
95          nvps.add(new BasicNameValuePair(CLIENT_NAME, place));
96          nvps.add(new BasicNameValuePair(SPACE_NAME, space));
97          nvps.add(new BasicNameValuePair(WORK_BUNDLE_ID, bundleId));
98          nvps.add(new BasicNameValuePair(WORK_BUNDLE_STATUS, Boolean.toString(itWorked)));
99          method.setEntity(new UrlEncodedFormEntity(nvps, StandardCharsets.UTF_8));
100         final EmissaryResponse status = send(method);
101         // TODO Look at putting this method in the EmissaryResponse
102         return status.getStatus() == HttpStatus.SC_OK;
103     }
104 
105 }