コード例 #1
0
ファイル: TestWorkflow.java プロジェクト: dana-i2cat/harmony
 /** Default constructor. */
 public TestWorkflow() {
   if (Config.isTrue("test", "test.callWebservice")) {
     final String epr = Config.getString("test", "test.reservationEPR");
     this.client = new SimpleReservationClient(epr);
   } else {
     this.client = new SimpleReservationClient(new ReservationWS());
   }
   this.logger = PhLogger.getLogger();
 }
コード例 #2
0
/**
 * This class implements a NRPS controller. It is used to communicate with an instance of an
 * Adapter/NRPS, so it contains the information required to contact it: IP and proxy to send the
 * messages, and other useful information.
 */
public class NRPSController extends Thread {

  /** Logger. */
  private final Logger logger = PhLogger.getLogger(this.getClass());

  /** Performance Logger. */
  private final Logger performanceLogger;

  /** Domain where the NRPS belongs to. */
  private final Domain domain;

  /** Proxy for the reservationEPR. */
  private SimpleReservationClient proxyRSV;

  /** RSV Web Service URI. */
  private EndpointReference wsRsvEpr;

  /** TOPO Web Service URI. */
  private EndpointReference wsTopoEpr;

  /** Operation to execute. */
  private final String op;

  /** Message to send. */
  private final Serializable msg;

  /** Result of the operation. */
  private Serializable result = null;

  /** Indicates if this Controller has to trigger a rollback */
  private boolean rollback = false;

  /** Stores the Exception (if any) */
  private SoapFault exception = null;

  /**
   * Constructor for the controller of an NRPS.
   *
   * @param dom Domain name
   * @param operation
   * @param message
   */
  public NRPSController(
      final Domain dom,
      final String operation,
      final Serializable message,
      final boolean malleable,
      final Logger performanceLogger) {

    this.op = operation;
    this.msg = message;
    this.domain = dom;
    this.performanceLogger = performanceLogger;
    this.setDaemon(true);
    this.setName(Thread.currentThread().getName() + "_NRPSController@" + dom.getName());

    this.wsRsvEpr = new EndpointReference(dom.getReservationURI());
    if (Config.isTrue(Constants.idbProperties, "passViaWebservice")) {
      this.proxyRSV = new SimpleReservationClient(this.wsRsvEpr);
    } else {
      if (malleable) {
        this.proxyRSV = new SimpleReservationClient(new MalleableReservationWS());
      } else {
        this.proxyRSV = new SimpleReservationClient(new ReservationWS());
      }
    }

    this.logger.info("NRPSController created for " + dom.getName());
  }

  /**
   * This Method is used to send an ActivationRequest message to the NRPS. It uses a request sender
   * that executes the work in a thread.
   *
   * @param request Message to send to the NRPSAdapter
   * @return Response from the NRPS
   */
  public void activateReservation(final ActivateType request) {

    this.start();
  }

  /**
   * This Method is used to send a CancelReservation message to the NRPS. It uses a request sender
   * that executes the work in a thread.
   *
   * @param request Message to send to the NRPSAdapter
   * @return Response from the NRPS
   */
  public void cancelReservation(final CancelReservationType request) {

    this.start();
  }

  /**
   * This Method is used to send a ReservationRequest message to the NRPS. It uses a request sender
   * that executes the work in a thread.
   *
   * @param request Message to send to the NRPSAdapter
   * @return Response from the NRPS
   */
  public void createReservation(final CreateReservationType request) {

    this.start();
  }

  @Override
  protected final void finalize() {
    this.interrupt();
    // Makes null the proxy to kill it in GC
    this.proxyRSV = null;
  }

  public Domain getDomain() {
    return this.domain;
  }

  public SoapFault getException() {
    return this.exception;
  }

  public Serializable getMsg() {
    return this.msg;
  }

  public SimpleReservationClient getProxyRSV() {
    return this.proxyRSV;
  }

  /** @param request */
  public void getReservations(final GetReservationsType request) {

    this.logger.info("NRPSController.GetReservations");

    this.start();
  }

  /**
   * Gives the result of the request to the NRPS.
   *
   * @return The result from the NRPS or null if there is no result
   */
  public final Serializable getResult() {

    if (this.result != null) {
      return this.result;
    }

    this.logger.error("Request Sender: received NULL result from NRPS");
    return null;
  }

  /**
   * This Method is used to send a getStatus message to the NRPS. It uses a request sender that
   * executes the work in a thread.
   *
   * @param request Message to send to the NRPSAdapter
   * @return Response from the NRPS
   */
  public void getStatus(final GetStatusType request) {

    this.start();
  }

  public EndpointReference getWsRsvEpr() {
    return this.wsRsvEpr;
  }

  public EndpointReference getWsTopoEpr() {
    return this.wsTopoEpr;
  }

  /**
   * This Method is used to send a isAvailable message to the NRPS. It uses a request sender that
   * executes the work in a thread.
   *
   * @param request Message to send to the NRPSAdapter
   * @return Response from the NRPS
   */
  public void isAvailable(final IsAvailableType request) {

    this.logger.info("NRPSController.ISAvailable");

    this.start();
  }

  public boolean isRollback() {
    return this.rollback;
  }

  public boolean isSetException() {
    return this.exception != null;
  }

  @Override
  public void run() {
    this.logger.info("Starting sender thread...");

    if (this.op.equalsIgnoreCase("createReservation")) {
      Element response;
      try {

        // this.logger.info("SENDER: sending CreateReservation
        // request");

        final CreateReservation create = new CreateReservation();
        create.setCreateReservation((CreateReservationType) this.msg);

        response =
            this.proxyRSV.createReservation(JaxbSerializer.getInstance().objectToElement(create));

        final CreateReservationResponse res =
            (CreateReservationResponse) JaxbSerializer.getInstance().elementToObject(response);

        // logger.info("SENDER: received CreateReservation response");

        this.result = res.getCreateReservationResponse();

      } catch (final SoapFault e) {
        this.rollback = true;
        this.exception = e;
        e.printStackTrace();
      }
    } else if (this.op.equalsIgnoreCase("activateReservation")) {
      Element response;
      try {
        final Activate act = new Activate();
        act.setActivate((ActivateType) this.msg);

        response = this.proxyRSV.activate(JaxbSerializer.getInstance().objectToElement(act));

        final ActivateResponse res =
            (ActivateResponse) JaxbSerializer.getInstance().elementToObject(response);

        this.result = res.getActivateResponse();

      } catch (final SoapFault e) {
        this.exception = e;
        e.printStackTrace();
      }
    } else if (this.op.equalsIgnoreCase("cancelReservation")) {
      Element response;
      try {

        final CancelReservation cancel = new CancelReservation();

        cancel.setCancelReservation((CancelReservationType) this.msg);

        response =
            this.proxyRSV.cancelReservation(JaxbSerializer.getInstance().objectToElement(cancel));

        final CancelReservationResponse res =
            (CancelReservationResponse) JaxbSerializer.getInstance().elementToObject(response);

        this.result = res.getCancelReservationResponse();

      } catch (final SoapFault e) {
        this.exception = e;
        e.printStackTrace();
      }
    } else if (this.op.equalsIgnoreCase("getStatus")) {
      Element response;
      try {
        final GetStatus get = new GetStatus();
        get.setGetStatus((GetStatusType) this.msg);

        response = this.proxyRSV.getStatus(JaxbSerializer.getInstance().objectToElement(get));

        final GetStatusResponse res =
            (GetStatusResponse) JaxbSerializer.getInstance().elementToObject(response);

        this.result = res.getGetStatusResponse();

      } catch (final SoapFault e) {
        this.exception = e;
        e.printStackTrace();
      }
    } else if (this.op.equalsIgnoreCase("isAvailable")) {
      Element response;
      try {

        final IsAvailable avail = new IsAvailable();
        avail.setIsAvailable((IsAvailableType) this.msg);

        response = this.proxyRSV.isAvailable(JaxbSerializer.getInstance().objectToElement(avail));

        // logger.info("Request Sender: received IsAvailable response");

        final IsAvailableResponse res =
            (IsAvailableResponse) JaxbSerializer.getInstance().elementToObject(response);

        this.result = res.getIsAvailableResponse();

      } catch (final SoapFault e) {
        this.exception = e;
        e.printStackTrace();
      }
    } else if (this.op.equalsIgnoreCase("getReservations")) {
      Element response;
      try {

        final GetReservations getRsvs = new GetReservations();
        getRsvs.setGetReservations((GetReservationsType) this.msg);

        response =
            this.proxyRSV.getReservations(JaxbSerializer.getInstance().objectToElement(getRsvs));

        final GetReservationsResponse res =
            (GetReservationsResponse) JaxbSerializer.getInstance().elementToObject(response);

        this.result = res.getGetReservationsResponse();

      } catch (final SoapFault e) {
        this.exception = e;
        e.printStackTrace();
      }
    }

    this.logger.info("finished");
    this.performanceLogger.log(
        PerformanceLogLevel.PERFORMANCE_LOG,
        "NRPS_response_time "
            + this.domain.getName()
            + " "
            + this.proxyRSV.getLastCallDuration()
            + "ms");
  }

  public void setProxyRSV(final SimpleReservationClient proxyRSV) {
    this.proxyRSV = proxyRSV;
  }

  public void setWsRsvEpr(final EndpointReference wsRsvEpr) {
    this.wsRsvEpr = wsRsvEpr;
  }

  public void setWsTopoEpr(final EndpointReference wsTopoEpr) {
    this.wsTopoEpr = wsTopoEpr;
  }
}
コード例 #3
0
ファイル: TokenHelper.java プロジェクト: dana-i2cat/harmony
public class TokenHelper {

  private static final HashMap<String, String> TOKEN = new HashMap<String, String>();

  private static final Logger log = PhLogger.getLogger();

  public static final String getTokenValue(final String token) throws SoapFault {
    try {
      Document docToken = XmlUtils.createDocument(token);

      NodeList values = docToken.getElementsByTagName(AAIConstants.TOKEN_VALUE);

      Node node = values.item(0);

      if (null == node || null == node.getTextContent()) {
        throw new InvalidRequestFaultException("Token does not contain any value");
      }

      final String value = node.getTextContent();

      TOKEN.put(value, token);

      return value;
    } catch (SoapFault e) {
      throw e;
    } catch (Exception e) {
      throw new InvalidRequestFaultException("Error while processing token: " + e.getMessage(), e);
    }
  }

  /**
   * Add Token to a specific jaxb object.
   *
   * @param string the serialized JaxbObject
   * @param token the token
   * @return
   * @throws SoapFault
   */
  public static final Document addTokenCreate(final Document doc, final String token)
      throws SoapFault {
    NodeList nodes =
        doc.getElementsByTagNameNS("http://ist_phosphorus.eu/nsp/webservice/reservation", "Token");

    if (nodes != null && nodes.getLength() > 0) {
      nodes.item(0).setTextContent(token);
    }
    return doc;
  }
  /**
   * Add Token to a specific jaxb object.
   *
   * @param string the serialized JaxbObject
   * @param token the token
   * @return
   * @throws SoapFault
   */
  public static final String getToken(final Document doc) throws SoapFault {
    NodeList nodes =
        doc.getElementsByTagNameNS("http://ist_phosphorus.eu/nsp/webservice/reservation", "Token");
    if (nodes == null || nodes.getLength() == 0) {
      nodes =
          doc.getElementsByTagNameNS(
              "http://ist_phosphorus.eu/nsp/webservice/reservation", "Token");
    }

    if (nodes != null && nodes.getLength() > 0) {
      String tokenRet = nodes.item(0).getTextContent();
      return tokenRet;
    }
    return null;
  }

  /**
   * Extract Token from Object.
   *
   * @param obj JaxbObject
   * @return Token, or null if Object has no Token field
   * @throws InvocationTargetException
   * @throws IllegalAccessException
   * @throws IllegalArgumentException
   */
  public static final String getToken(final Object obj)
      throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    String token = null;

    try {
      final Method getToken = obj.getClass().getMethod("getToken");

      token = (String) getToken.invoke(obj);
    } catch (final NoSuchMethodException nsme) {
      TokenHelper.log.error(
          "The request: " + obj.getClass().getCanonicalName() + " contains no token!", nsme);
    }

    return token;
  }

  /**
   * @param val
   * @return
   * @throws OperationNotAllowedFaultException
   */
  public static final String getFullTokenByValue(final String val)
      throws OperationNotAllowedFaultException {
    final String result = TOKEN.get(val);

    if (null == result) {
      throw new OperationNotAllowedFaultException("Invalid Token!");
    }

    return result;
  }

  public static Document replaceTokenValue(Document dom, String tokenValue) {

    NodeList listNode =
        dom.getElementsByTagNameNS("http://ist_phosphorus.eu/nsp/webservice/reservation", "Token");
    if (listNode == null || listNode.getLength() == 0) {
      listNode =
          dom.getElementsByTagNameNS(
              "http://ist_phosphorus.eu/nsp/webservice/reservation", "Token");
    }
    if (listNode != null && listNode.getLength() > 0) {
      TokenHelper.log.info("Getting Token...");
      TokenHelper.log.info(listNode.item(0).getTextContent());
      listNode.item(0).setTextContent(tokenValue);
      return dom;
    }
    return null;
  }
}