/* This function performs the decoding of the authentication header */
  public void decodeAuthorizationHeader() {
    // check if this request has basic authentication
    if (!authHeader.contains("Basic ")) {
      throw new WebApplicationException(Response.Status.BAD_REQUEST);
    }

    authHeader = authHeader.substring("Basic ".length());
    String[] decodedHeader;
    decodedHeader = Base64.base64Decode(authHeader).split(":");

    if (decodedHeader == null) {
      throw new WebApplicationException(Response.Status.BAD_REQUEST);
    }

    oAuthenticationAccount.setusername(decodedHeader[0]);
    oAuthenticationAccount.setpassword(decodedHeader[1]);
  }
 public GetpurchaseListHandler(String authHeader, int accountId, UriInfo oApplicationUri) {
   this.oHibernateController = HibernateController.getHibernateControllerHandle();
   this.oApplicationUri = oApplicationUri;
   oJavaaccountModel = new JavaaccountModel();
   oJavaaccountModel.setaccountId(accountId);
   this.authHeader = authHeader;
   this.oAuthenticationAccount = new JavaaccountModel();
 }
  /* This function produces hypermedia links to be sent to the client so as it will be able to forward the application state in a valid way.*/
  public JavapurchaseModelManager createHypermedia(JavaaccountModel oJavaaccountModel) {
    JavapurchaseModelManager oJavapurchaseModelManager = new JavapurchaseModelManager();

    /* Create hypermedia links towards this specific purchase resource. These must be GET and POST as it is prescribed in the meta-models.*/
    oJavapurchaseModelManager
        .getlinklist()
        .add(
            new HypermediaLink(
                String.format("%s%s", oApplicationUri.getBaseUri(), oApplicationUri.getPath()),
                "Get all purchases of this account",
                "GET",
                "Sibling"));
    oJavapurchaseModelManager
        .getlinklist()
        .add(
            new HypermediaLink(
                String.format("%s%s", oApplicationUri.getBaseUri(), oApplicationUri.getPath()),
                "Create a new purchase",
                "POST",
                "Sibling"));

    /* Then calculate the relative path to any related resource of this one and add for each one a hypermedia link to the Linklist.*/
    String oRelativePath;
    oRelativePath = oApplicationUri.getPath();
    Iterator<JavapurchaseModel> setIterator =
        oJavaaccountModel.getSetOfJavapurchaseModel().iterator();
    while (setIterator.hasNext()) {
      JavapurchaseModel oNextJavapurchaseModel = new JavapurchaseModel();
      oNextJavapurchaseModel = setIterator.next();
      oJavapurchaseModelManager
          .getlinklist()
          .add(
              new HypermediaLink(
                  String.format(
                      "%s%s/%d",
                      oApplicationUri.getBaseUri(),
                      oRelativePath,
                      oNextJavapurchaseModel.getpurchaseId()),
                  String.format("%s", oNextJavapurchaseModel.getsku()),
                  "GET",
                  "Child",
                  oNextJavapurchaseModel.getpurchaseId()));
    }

    /* Finally calculate the relative path towards the resources of which this one is related and add one hypermedia link for each one of them in the Linklist.*/
    oRelativePath = oApplicationUri.getPath();
    int iLastSlashIndex =
        String.format("%s%s", oApplicationUri.getBaseUri(), oRelativePath).lastIndexOf("/");
    oJavapurchaseModelManager
        .getlinklist()
        .add(
            new HypermediaLink(
                String.format("%s%s", oApplicationUri.getBaseUri(), oRelativePath)
                    .substring(0, iLastSlashIndex),
                "Delete the parent JavaaccountModel",
                "DELETE",
                "Parent"));
    oJavapurchaseModelManager
        .getlinklist()
        .add(
            new HypermediaLink(
                String.format("%s%s", oApplicationUri.getBaseUri(), oRelativePath)
                    .substring(0, iLastSlashIndex),
                "Get the parent JavaaccountModel",
                "GET",
                "Parent"));
    oJavapurchaseModelManager
        .getlinklist()
        .add(
            new HypermediaLink(
                String.format("%s%s", oApplicationUri.getBaseUri(), oRelativePath)
                    .substring(0, iLastSlashIndex),
                "Update the JavaaccountModel",
                "PUT",
                "Parent"));
    return oJavapurchaseModelManager;
  }