Package cave.nice.testMessage.data

Examples of cave.nice.testMessage.data.DataManager


  private UriInfo uriInfo;

  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public Response getVerifiedAccounts() throws WebApplicationException {
    DataManager dataManager = getDataManager();
    try {
      return Response.ok(dataManager.getVerifiedAccounts()).build();
    } catch (CannotRetrieveEntitiesException e) {
      // TODO Log error, not return it
      throw new WebApplicationException(Response.serverError().entity(e)
          .build());
    } finally {
      dataManager.close();
    }
  }
View Full Code Here


      @DefaultValue("GMail") @FormParam("medium") String reportingMediumString) {
    ReportType reportType = ReportType.valueOf(reportTypeString);
    ReportingMedium reportingMedium = ReportingMedium
        .valueOf(reportingMediumString);

    DataManager dataManager = getDataManager();
    try {
      VerifiedAccount account = dataManager
          .getVerifiedAccount(emailAddress);
      List<Test> openTests = dataManager.getOpenTests(account);

      final String report;
      try {
        report = ReportsGenerator.getReport(getCurrentUser(), account,
            openTests, reportType, dataManager.getCurrentTime());
      } catch (Exception e) {
        // TODO Log error
        return Response
            .serverError()
            .entity("Error while generating the report for the account: "
                + account.getEmailAddress()).build();
      }

      switch (reportingMedium) {
      case GMail: {
        InternetAddress appInternetAddress = null;
        try {
          appInternetAddress = new InternetAddress(
              TestMessageConstants.NOTIFICATIONS_EMAIL_ADDRESS,
              TestMessageConstants.TEST_MESSAGE_SENDER_NAME);
        } catch (UnsupportedEncodingException e) {
          throw new WebApplicationException(e);
        }

        Session session = Session.getDefaultInstance(new Properties(),
            null);

        String notificationMailAddress = getCurrentUser().getEmail();
        try {
          MimeMessage msg = new MimeMessage(session);
          msg.setFrom(appInternetAddress);
          msg.addRecipient(Message.RecipientType.TO,
              new InternetAddress(notificationMailAddress));
          msg.setReplyTo(new InternetAddress[] { appInternetAddress });
          msg.setSubject("Test Message Report for the account '"
              + account.getEmailAddress() + "'");
          msg.setContent(report, "text/html");

          Transport.send(msg);
        } catch (Exception e) {
          // TODO Log error
          return Response
              .serverError()
              .entity("Error while sending the report to '"
                  + notificationMailAddress + "'").build();
        }

        break;
      }
      case GTalk: {
        JID jid = new JID(getCurrentUser().getEmail());
        com.google.appengine.api.xmpp.Message xmppMessage = new MessageBuilder()
            .withRecipientJids(jid).withBody(report).build();

        XMPPService xmpp = XMPPServiceFactory.getXMPPService();
        SendResponse status = xmpp.sendMessage(xmppMessage);
        boolean messageSent = (status.getStatusMap().get(jid) == SendResponse.Status.SUCCESS);

        if (!messageSent) {
          /*
           * TODO Retry later?
           */
          throw new WebApplicationException(
              Response.serverError()
                  .entity("Cannot send the report over XMPP for the account '"
                      + emailAddress
                      + "' on the JID '"
                      + jid + "'").build());
        }

        break;
      }
      }

      return Response.noContent().build();
    } catch (UnknownVerifiedAccountException e) {
      throw new WebApplicationException(Response
          .status(Status.NOT_FOUND)
          .entity("No account '" + emailAddress.getAddress()
              + "' found").build());
    } catch (CannotRetrieveEntitiesException e) {
      throw new WebApplicationException(Response.serverError().entity(e)
          .build());
    } finally {
      dataManager.close();
    }
  }
View Full Code Here

  @Produces(MediaType.APPLICATION_JSON)
  public Response getJSONReport(
      @PathParam("emailAddress") InternetAddress emailAddress,
      @QueryParam("since") Date sinceDate) {
    // TODO Implement since
    DataManager dataManager = getDataManager();
    try {
      VerifiedAccount account = dataManager
          .getVerifiedAccount(emailAddress);
      Report report = new Report();
      report.setTimestamp(new Date());
      report.setOpenTests(Lists.newLinkedList(dataManager.getOpenTests(account)));
      report.setClosedTests(Lists.newLinkedList(dataManager.getClosedTests(account)));
      return Response.ok(report).type(MediaType.APPLICATION_JSON).build();
    } catch (UnknownVerifiedAccountException e) {
      throw new WebApplicationException(Response
          .status(Status.NOT_FOUND)
          .entity("No account '" + emailAddress.getAddress()
              + "' found").build());
    } catch (CannotRetrieveEntitiesException e) {
      throw new WebApplicationException(Response.serverError().entity(e)
          .build());
    } finally {
      dataManager.close();
    }
  } 
View Full Code Here

      reportType = ReportType.TEXT_HTML;
    } else if (mediaType == MediaType.TEXT_PLAIN_TYPE) {
      reportType = ReportType.TEXT_PLAIN;
    }

    DataManager dataManager = getDataManager();
    try {
      VerifiedAccount account = dataManager
          .getVerifiedAccount(emailAddress);
      return Response.ok(
          ReportsGenerator.getReport(getCurrentUser(), account,
              dataManager.getOpenTests(account), reportType,
              dataManager.getCurrentTime()), mediaType).build();
    } catch (UnknownVerifiedAccountException e) {
      throw new WebApplicationException(Response
          .status(Status.NOT_FOUND)
          .entity("No account '" + emailAddress.getAddress()
              + "' found").build());
    } catch (CannotRetrieveEntitiesException e) {
      throw new WebApplicationException(Response.serverError().entity(e)
          .build());
    } finally {
      dataManager.close();
    }
  }
View Full Code Here

  @Path("/{emailAddress}/tests")
  public void answerTest(
      @PathParam("emailAddress") InternetAddress emailAddress,
      @FormParam("challenge") UUID challenge)
      throws WebApplicationException {
    DataManager dataManager = getDataManager();
    try {
      VerifiedAccount account = dataManager
          .getVerifiedAccount(emailAddress);
      Test test = dataManager.getTestOfAccount(account, challenge);
      dataManager.markTestAsAnswered(test, new Date());
      LOGGER.info("REST answer to the test with challenge '"
          + test.getChallenge() + "' of the email account '"
          + account.getEmailAddress() + "'");
    } catch (UknownTestChallengeException e) {
      throw new WebApplicationException(Response
          .status(Status.NOT_FOUND)
          .entity("No test with challenge '" + challenge
              + "' is found").build());
    } catch (WrongAccountForTestException e) {
      throw new WebApplicationException(Response
          .status(Status.FORBIDDEN)
          .entity("The test with challenge '" + challenge
              + "' does not belong to the account '"
              + emailAddress.getAddress() + "'").build());
    } catch (CannotUpdateEntityException e) {
      throw new WebApplicationException(Response.serverError().entity(e)
          .build());
    } catch (UnknownVerifiedAccountException e) {
      throw new WebApplicationException(Response
          .status(Status.NOT_FOUND)
          .entity("No account '" + emailAddress.getAddress()
              + "' found").build());
    } catch (TestAlreadyAnsweredException e) {
      throw new WebApplicationException(Response
          .status(Status.BAD_REQUEST)
          .entity("The test with challenge '" + challenge
              + "' has already been answered").build());
    } finally {
      dataManager.close();
    }
  }
View Full Code Here

  public Response addTest(@Context ServletContext context,
      @PathParam("emailAddress") InternetAddress emailAddress,
      @DefaultValue("true") @FormParam("sendTest") Boolean sendTest)
      throws WebApplicationException {
    UUID challenge = UUID.randomUUID();
    DataManager dataManager = getDataManager();

    try {
      VerifiedAccount account = dataManager
          .getVerifiedAccount(emailAddress);
      Test newTest = dataManager.createTest(account, new Date(),
          challenge);

      if (sendTest) {
        Session session = Session.getDefaultInstance(new Properties(),
            null);
        Message msg = null;
        try {
          InternetAddress appInternetAddress = new InternetAddress(
              TestMessageConstants.TESTS_EMAIL_ADDRESS,
              TestMessageConstants.TEST_MESSAGE_SENDER_NAME);

          Map<String, Object> params = Maps.newHashMap();
          params.put("challenge", newTest.getChallenge());
          params.put(
              "url",
              "http://"
                  + SystemProperty.applicationId.get()
                  + ".appspot.com/answerTest.jsp?emailAddress="
                  + account.getEmailAddress() + "&challenge="
                  + newTest.getChallenge());

          String content = TemplateManager.getInstance(context)
              .applyTemplate(TemplateType.TEST_MESSAGE, params);

          msg = new MimeMessage(session);
          msg.setFrom(appInternetAddress);
          msg.addRecipient(Message.RecipientType.TO,
              account.getInternetAddress());
          msg.setReplyTo(new InternetAddress[] { appInternetAddress });
          msg.addHeader(
              TestMessageConstants.SMTP_HEADER_ACCOUNT_IDENTIFIER,
              KeyFactory.keyToString(account.getIdentifier()));
          msg.addHeader(
              TestMessageConstants.SMTP_HEADER_TEST_IDENTIFIER,
              KeyFactory.keyToString(newTest.getIdentifier()));
          msg.setSubject("Test Message on "
              + DateFormat.getInstance()
                  .format(newTest.getDate()));
          msg.setContent(content, "text/plain");
          Transport.send(msg);
        } catch (Exception e) {
          /*
           * Remove the test that was not successfully sent
           */
          dataManager.removeTest(newTest);

          throw new WebApplicationException(
              Response.serverError()
                  .entity("Error while sending the following test message to '"
                      + account.getEmailAddress()
                      + "': "
                      + msg).entity(e).build());
        }
      }

      URI newEntityURI = uriInfo.getBaseUri().resolve(
          "/resources/accounts/" + emailAddress.getAddress()
              + "/tests/" + challenge);

      return Response.created(newEntityURI).build();
    } catch (UnknownVerifiedAccountException e) {
      throw new WebApplicationException(Response
          .status(Status.NOT_FOUND)
          .entity("No account '" + emailAddress.getAddress()
              + "' found").build());
    } finally {
      dataManager.close();
    }
  }
View Full Code Here

  @Path("/{emailAddress}/tests")
  @Produces(MediaType.APPLICATION_JSON)
  public List<Test> getAllTests(
      @PathParam("emailAddress") InternetAddress emailAddress)
      throws WebApplicationException {
    DataManager dataManager = getDataManager();
    try {
      VerifiedAccount account = dataManager
          .getVerifiedAccount(emailAddress);

      return Lists.newArrayList(account.getTests());
    } catch (UnknownVerifiedAccountException e) {
      throw new WebApplicationException(Response
          .status(Status.NOT_FOUND)
          .entity("No account '" + emailAddress.getAddress()
              + "' found").build());
    } finally {
      dataManager.close();
    }
  }
View Full Code Here

  @Path("/{emailAddress}/closedTests")
  @Produces(MediaType.APPLICATION_JSON)
  public List<Test> getClosedTests(
      @PathParam("emailAddress") InternetAddress emailAddress)
      throws WebApplicationException {
    DataManager dataManager = getDataManager();
    try {
      VerifiedAccount account = dataManager
          .getVerifiedAccount(emailAddress);
      // checkAuthentication(userOwnerOf(account).or(userIsAdministrator()));

      return Lists.newArrayList(dataManager.getClosedTests(account));
    } catch (CannotRetrieveEntitiesException e) {
      throw new WebApplicationException(
          Response.serverError()
              .entity("Error while retrieving the closed tests of the account '"
                  + emailAddress.getAddress() + "'")
              .entity(e).build());
    } catch (UnknownVerifiedAccountException e) {
      throw new WebApplicationException(Response
          .status(Status.NOT_FOUND)
          .entity("No account '" + emailAddress.getAddress()
              + "' found").build());
    } finally {
      dataManager.close();
    }
  }
View Full Code Here

  @Path("/{emailAddress}/openTests")
  @Produces(MediaType.APPLICATION_JSON)
  public List<Test> getOpenTests(
      @PathParam("emailAddress") InternetAddress emailAddress)
      throws WebApplicationException {
    DataManager dataManager = getDataManager();

    try {
      VerifiedAccount account = dataManager
          .getVerifiedAccount(emailAddress);

      return Lists.newArrayList(dataManager.getOpenTests(account));
    } catch (CannotRetrieveEntitiesException e) {
      throw new WebApplicationException(
          Response.serverError()
              .entity("Error while retrieving the open tests of the account '"
                  + emailAddress.getAddress() + "'")
              .entity(e).build());
    } catch (UnknownVerifiedAccountException e) {
      throw new WebApplicationException(Response
          .status(Status.NOT_FOUND)
          .entity("No account '" + emailAddress.getAddress()
              + "' found").build());
    } finally {
      dataManager.close();
    }
  }
View Full Code Here

  @Path("/{emailAddress}/tests/{challenge}")
  public Response deleteTest(
      @PathParam("emailAddress") InternetAddress emailAddress,
      @PathParam("challenge") UUID challenge)
      throws WebApplicationException {
    DataManager dataManager = getDataManager();
    try {
      VerifiedAccount account = dataManager
          .getVerifiedAccount(emailAddress);

      Test test = dataManager.getTestOfAccount(account, challenge);
      dataManager.removeTest(test);
      return Response.ok().build();
    } catch (UknownTestChallengeException e) {
      throw new WebApplicationException(Response
          .status(Status.NOT_MODIFIED)
          .entity("No test with challenge '" + challenge + "' found")
          .build());
    } catch (WrongAccountForTestException e) {
      throw new WebApplicationException(Status.FORBIDDEN);
    } catch (UnknownVerifiedAccountException e) {
      throw new WebApplicationException(Response
          .status(Status.NOT_FOUND)
          .entity("No account '" + emailAddress.getAddress()
              + "' found").build());
    } finally {
      dataManager.close();
    }
  }
View Full Code Here

TOP

Related Classes of cave.nice.testMessage.data.DataManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.