Exemplo n.º 1
0
  @Override
  public boolean login(String username, String password) throws ServiceException {
    RestTemplate restTemplate = this.restClient.getRestTemplate();
    String url = this.restClient.createServiceUrl("/login");
    HttpHeaders headers = this.restClient.getHttpHeaders();
    headers.add("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);

    MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
    form.add("user", username);
    form.add("password", password);

    HttpEntity<MultiValueMap<String, String>> entity =
        new HttpEntity<MultiValueMap<String, String>>(form, headers);

    LOG.info("Login " + username + " at " + url);

    UserStatusDto status = null;
    try {
      status = restTemplate.postForObject(url, entity, UserStatusDto.class);
    } catch (RestClientException e) {
      throw new ServiceException("Login failed: " + e.getMessage(), e);
    }

    if (status.getEvent() == UserEvent.AUTH_FAILURE) {
      return false;
    }

    LOG.debug("Login " + username + " successful");

    this.user = status;

    return true;
  }
Exemplo n.º 2
0
  @Override
  public void logout() throws ServiceException {
    RestTemplate restTemplate = this.restClient.getRestTemplate();
    String url = this.restClient.createServiceUrl("/logout");

    LOG.info("Logout " + this.user.getUsername() + " at " + url);

    UserStatusDto status = null;
    try {
      this.updateLastLoginDate(this.user.getId());
      status = restTemplate.getForObject(url, UserStatusDto.class);
    } catch (RestClientException e) {
      throw new ServiceException("Logout failed: " + e.getMessage(), e);
    }

    if (status.getEvent() != UserEvent.LOGOUT) {
      throw new ServiceException("Logout failed: Invalid event");
    } else {
      LOG.debug("Logout " + this.user.getUsername() + " successful");
    }

    this.user = new UserStatusDto();
  }