diff --git a/src/main/java/org/broadinstitute/dsm/model/gbf/CancelRequest.java b/src/main/java/org/broadinstitute/dsm/model/gbf/CancelRequest.java new file mode 100644 index 000000000..e1eee73b8 --- /dev/null +++ b/src/main/java/org/broadinstitute/dsm/model/gbf/CancelRequest.java @@ -0,0 +1,13 @@ +package org.broadinstitute.dsm.model.gbf; + +import com.google.gson.annotations.SerializedName; + +public class CancelRequest { + + @SerializedName("orderNumber") + private String orderNumber; + + public CancelRequest(String orderNumber) { + this.orderNumber = orderNumber; + } +} diff --git a/src/main/java/org/broadinstitute/dsm/model/gbf/CancelResponse.java b/src/main/java/org/broadinstitute/dsm/model/gbf/CancelResponse.java new file mode 100644 index 000000000..44fee729a --- /dev/null +++ b/src/main/java/org/broadinstitute/dsm/model/gbf/CancelResponse.java @@ -0,0 +1,34 @@ +package org.broadinstitute.dsm.model.gbf; + +import com.google.gson.annotations.SerializedName; + +public class CancelResponse { + + @SerializedName("orderNumber") + private String orderNumber; + + @SerializedName("orderStatus") + private String orderStatus; + + @SerializedName("success") + private Boolean success; + + @SerializedName("errorMessage") + private String errorMessage; + + public String getOrderNumber() { + return orderNumber; + } + + public String getOrderStatus() { + return orderStatus; + } + + public boolean wasSuccessful() { + return Boolean.TRUE.equals(success); + } + + public String getErrorMessage() { + return errorMessage; + } +} diff --git a/src/main/java/org/broadinstitute/dsm/util/externalShipper/CancelGBFOrderCLI.java b/src/main/java/org/broadinstitute/dsm/util/externalShipper/CancelGBFOrderCLI.java new file mode 100644 index 000000000..5bc634ef7 --- /dev/null +++ b/src/main/java/org/broadinstitute/dsm/util/externalShipper/CancelGBFOrderCLI.java @@ -0,0 +1,52 @@ +package org.broadinstitute.dsm.util.externalShipper; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.io.IOUtils; +import org.broadinstitute.dsm.model.gbf.CancelResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CancelGBFOrderCLI { + + private static final Logger logger = LoggerFactory.getLogger(CancelGBFOrderCLI.class); + + + public static void main(String[] args) { + String cancelUrl = args[0]; + String apiKey = args[1]; + String ordersFile = args[2]; + FileReader ordersReader = null; + List ordersToCancel = new ArrayList<>(); + + try { + ordersReader = new FileReader(new File(ordersFile)); + ordersToCancel = IOUtils.readLines(ordersReader); + } catch(IOException e) { + logger.error("Could read orders from file " + ordersFile,e); + } + + if (ordersReader != null) { + CancelResponse cancelResponse = null; + for (String orderNumber : ordersToCancel) { + try { + cancelResponse = GBFRequestUtil.cancelOrder(orderNumber, cancelUrl, apiKey); + if (cancelResponse.wasSuccessful()) { + logger.info("Cancelled order {}", orderNumber); + } else { + logger.error("Could not cancel order {} due to {}",orderNumber, cancelResponse.getErrorMessage()); + } + } catch(Exception e) { + logger.error("Could not cancel order {}", orderNumber, e); + } + } + } + + + } +} diff --git a/src/main/java/org/broadinstitute/dsm/util/externalShipper/GBFRequestUtil.java b/src/main/java/org/broadinstitute/dsm/util/externalShipper/GBFRequestUtil.java index 42a079b90..bb35c1c92 100644 --- a/src/main/java/org/broadinstitute/dsm/util/externalShipper/GBFRequestUtil.java +++ b/src/main/java/org/broadinstitute/dsm/util/externalShipper/GBFRequestUtil.java @@ -2,6 +2,7 @@ import com.google.api.client.http.HttpStatusCodes; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import lombok.NonNull; import org.apache.commons.lang3.NotImplementedException; import org.apache.commons.lang3.StringUtils; @@ -96,15 +97,17 @@ public class GBFRequestUtil implements ExternalShipper { private static Executor blindTrustEverythingExecutor; - public GBFRequestUtil() { + static { + initTrust(); + } + + private static synchronized void initTrust() { try { if (blindTrustEverythingExecutor == null) { blindTrustEverythingExecutor = Executor.newInstance(SecurityUtil.buildHttpClient()); } - } - catch (Exception e) { + } catch (Exception e) { logger.error("Starting up the blindTrustEverythingExecutor ", e); - System.exit(-3); } } @@ -615,4 +618,23 @@ public static void markKitRequestAsOrdered(ArrayList kitRequests) { } + public static CancelResponse cancelOrder(String orderNumber, String cancelUrl, String apiKey) throws Exception { + Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create(); + CancelRequest cancelRequest = new CancelRequest(orderNumber); + logger.info("About to cancel GBF order {}", orderNumber); + CancelResponse cancelResponse = executePost(CancelResponse.class, cancelUrl, gson.toJson(cancelRequest), apiKey); + + if (cancelResponse != null) { + if (cancelResponse.wasSuccessful()) { + logger.info("Cancelled GBF order {}", orderNumber); + } else { + logger.error("Could not cancel GBF order " + orderNumber + " due to " + cancelResponse.getErrorMessage()); + } + } else { + logger.error("Got no response after cancelling GBF order " + orderNumber); + } + return cancelResponse; + } + + }