Example usage for org.springframework.http HttpHeaders LOCATION

List of usage examples for org.springframework.http HttpHeaders LOCATION

Introduction

In this page you can find the example usage for org.springframework.http HttpHeaders LOCATION.

Prototype

String LOCATION

To view the source code for org.springframework.http HttpHeaders LOCATION.

Click Source Link

Document

The HTTP Location header field name.

Usage

From source file:com.netflix.genie.web.controllers.JobRestControllerIntegrationTests.java

/**
 * Test the job submit method for when the job is killed as it times out.
 *
 * @throws Exception If there is a problem.
 *//*ww  w.j ava2s  . c  om*/
@Test
public void testSubmitJobMethodKillOnTimeout() throws Exception {
    Assume.assumeTrue(SystemUtils.IS_OS_UNIX);
    final String commandArgs = "-c 'sleep 60'";

    final List<ClusterCriteria> clusterCriteriaList = new ArrayList<>();
    final Set<String> clusterTags = new HashSet<>();
    clusterTags.add("localhost");
    final ClusterCriteria clusterCriteria = new ClusterCriteria(clusterTags);
    clusterCriteriaList.add(clusterCriteria);

    final Set<String> commandCriteria = new HashSet<>();
    commandCriteria.add("bash");
    final JobRequest jobRequest = new JobRequest.Builder(JOB_NAME, JOB_USER, JOB_VERSION, commandArgs,
            clusterCriteriaList, commandCriteria).withTimeout(5).withDisableLogArchival(true).build();

    final MvcResult result = this.mvc.perform(MockMvcRequestBuilders.post(JOBS_API)
            .contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsBytes(jobRequest)))
            .andReturn();

    if (result.getResponse().getStatus() != HttpStatus.ACCEPTED.value()) {
        log.error("RESPONSE WASN'T 202 IT WAS: {} AND THE ERROR MESSAGE IS: {} AND THE CONTENT IS {}",
                result.getResponse().getStatus(), result.getResponse().getErrorMessage(),
                result.getResponse().getContentAsString());
        Assert.fail();
    }

    final String id = this.getIdFromLocation(result.getResponse().getHeader(HttpHeaders.LOCATION));

    this.waitForDone(id);

    this.mvc.perform(MockMvcRequestBuilders.get(JOBS_API + "/{id}", id))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
            .andExpect(MockMvcResultMatchers.jsonPath(ID_PATH, Matchers.is(id)))
            .andExpect(MockMvcResultMatchers.jsonPath(STATUS_PATH, Matchers.is(JobStatus.KILLED.toString())));
}

From source file:com.netflix.genie.web.controllers.JobRestControllerIntegrationTests.java

/**
 * Test the job submit method for when the job fails.
 *
 * @throws Exception If there is a problem.
 *//*from   ww  w  .  ja v  a 2 s .  c  o m*/
@Test
public void testSubmitJobMethodFailure() throws Exception {
    Assume.assumeTrue(SystemUtils.IS_OS_UNIX);
    final String commandArgs = "-c 'exit 1'";

    final List<ClusterCriteria> clusterCriteriaList = new ArrayList<>();
    final Set<String> clusterTags = new HashSet<>();
    clusterTags.add("localhost");
    final ClusterCriteria clusterCriteria = new ClusterCriteria(clusterTags);
    clusterCriteriaList.add(clusterCriteria);

    final Set<String> commandCriteria = new HashSet<>();
    commandCriteria.add("bash");
    final JobRequest jobRequest = new JobRequest.Builder(JOB_NAME, JOB_USER, JOB_VERSION, commandArgs,
            clusterCriteriaList, commandCriteria).withDisableLogArchival(true).build();

    final MvcResult result = this.mvc
            .perform(MockMvcRequestBuilders.post(JOBS_API).contentType(MediaType.APPLICATION_JSON)
                    .content(this.objectMapper.writeValueAsBytes(jobRequest)))
            .andExpect(MockMvcResultMatchers.status().isAccepted())
            .andExpect(MockMvcResultMatchers.header().string(HttpHeaders.LOCATION, Matchers.notNullValue()))
            .andReturn();

    final String id = this.getIdFromLocation(result.getResponse().getHeader(HttpHeaders.LOCATION));
    this.waitForDone(id);
    Assert.assertEquals(this.getStatus(id), "{\"status\":\"FAILED\"}");

    this.mvc.perform(MockMvcRequestBuilders.get(JOBS_API + "/{id}", id))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
            .andExpect(MockMvcResultMatchers.jsonPath(ID_PATH, Matchers.is(id)))
            .andExpect(MockMvcResultMatchers.jsonPath(STATUS_PATH, Matchers.is(JobStatus.FAILED.toString())));
}

From source file:com.netflix.genie.web.controllers.RestControllerIntegrationTestBase.java

<R extends ExecutionEnvironmentDTO> String createConfigResource(@NotNull final R resource,
        @Nullable final RestDocumentationFilter documentationFilter) throws Exception {
    final String endpoint;
    if (resource instanceof Application) {
        endpoint = APPLICATIONS_API;//from  w  w w.  j  ava2  s . c  o  m
    } else if (resource instanceof Cluster) {
        endpoint = CLUSTERS_API;
    } else if (resource instanceof Command) {
        endpoint = COMMANDS_API;
    } else {
        throw new IllegalArgumentException("Unexpected type: " + resource.getClass().getCanonicalName());
    }

    final RequestSpecification configRequestSpec = RestAssured.given(this.requestSpecification)
            .contentType(MediaType.APPLICATION_JSON_VALUE)
            .body(GenieObjectMapper.getMapper().writeValueAsBytes(resource));

    if (documentationFilter != null) {
        configRequestSpec.filter(documentationFilter);
    }

    return this.getIdFromLocation(configRequestSpec.when().port(this.port).post(endpoint).then()
            .statusCode(Matchers.is(HttpStatus.CREATED.value()))
            .header(HttpHeaders.LOCATION, Matchers.notNullValue()).extract().header(HttpHeaders.LOCATION));
}

From source file:org.ambraproject.wombat.controller.ArticleAssetController.java

/**
 * Redirect to a given link. (We can't just return a {@link org.springframework.web.servlet.view.RedirectView} because
 * we ordinarily want to pass the raw response to {@link #forwardAssetResponse}. So we mess around with it directly.)
 */// w  ww  .j av  a2 s.  co m
private void redirectTo(HttpServletResponse response, String location) {
    response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
    response.setHeader(HttpHeaders.LOCATION, location);
}

From source file:org.thingsboard.server.controller.AbstractControllerTest.java

protected User createUserAndLogin(User user, String password) throws Exception {
    User savedUser = doPost("/api/user", user, User.class);
    logout();/*from  www .  java  2  s  . co m*/
    doGet("/api/noauth/activate?activateToken={activateToken}", TestMailService.currentActivateToken)
            .andExpect(status().isSeeOther()).andExpect(header().string(HttpHeaders.LOCATION,
                    "/login/createPassword?activateToken=" + TestMailService.currentActivateToken));
    JsonNode activateRequest = new ObjectMapper().createObjectNode()
            .put("activateToken", TestMailService.currentActivateToken).put("password", password);
    JsonNode tokenInfo = readResponse(
            doPost("/api/noauth/activate", activateRequest).andExpect(status().isOk()), JsonNode.class);
    validateAndSetJwtToken(tokenInfo, user.getEmail());
    return savedUser;
}

From source file:org.thingsboard.server.controller.BaseUserControllerTest.java

@Test
public void testSaveUser() throws Exception {
    loginSysAdmin();/* w w  w  . ja v a  2 s. c  o  m*/

    Tenant tenant = new Tenant();
    tenant.setTitle("My tenant");
    Tenant savedTenant = doPost("/api/tenant", tenant, Tenant.class);
    Assert.assertNotNull(savedTenant);

    String email = "tenant2@thingsboard.org";
    User user = new User();
    user.setAuthority(Authority.TENANT_ADMIN);
    user.setTenantId(savedTenant.getId());
    user.setEmail(email);
    user.setFirstName("Joe");
    user.setLastName("Downs");
    User savedUser = doPost("/api/user", user, User.class);
    Assert.assertNotNull(savedUser);
    Assert.assertNotNull(savedUser.getId());
    Assert.assertTrue(savedUser.getCreatedTime() > 0);
    Assert.assertEquals(user.getEmail(), savedUser.getEmail());

    User foundUser = doGet("/api/user/" + savedUser.getId().getId().toString(), User.class);
    Assert.assertEquals(foundUser, savedUser);

    logout();
    doGet("/api/noauth/activate?activateToken={activateToken}", TestMailService.currentActivateToken)
            .andExpect(status().isSeeOther()).andExpect(header().string(HttpHeaders.LOCATION,
                    "/login/createPassword?activateToken=" + TestMailService.currentActivateToken));

    JsonNode activateRequest = new ObjectMapper().createObjectNode()
            .put("activateToken", TestMailService.currentActivateToken).put("password", "testPassword");

    JsonNode tokenInfo = readResponse(
            doPost("/api/noauth/activate", activateRequest).andExpect(status().isOk()), JsonNode.class);
    validateAndSetJwtToken(tokenInfo, email);

    doGet("/api/auth/user").andExpect(status().isOk())
            .andExpect(jsonPath("$.authority", is(Authority.TENANT_ADMIN.name())))
            .andExpect(jsonPath("$.email", is(email)));

    logout();

    login(email, "testPassword");

    doGet("/api/auth/user").andExpect(status().isOk())
            .andExpect(jsonPath("$.authority", is(Authority.TENANT_ADMIN.name())))
            .andExpect(jsonPath("$.email", is(email)));

    loginSysAdmin();
    doDelete("/api/user/" + savedUser.getId().getId().toString()).andExpect(status().isOk());

    doDelete("/api/tenant/" + savedTenant.getId().getId().toString()).andExpect(status().isOk());
}

From source file:org.thingsboard.server.controller.BaseUserControllerTest.java

@Test
public void testResetPassword() throws Exception {
    loginSysAdmin();/* ww w .j av  a2  s  .c  o  m*/

    Tenant tenant = new Tenant();
    tenant.setTitle("My tenant");
    Tenant savedTenant = doPost("/api/tenant", tenant, Tenant.class);
    Assert.assertNotNull(savedTenant);

    String email = "tenant2@thingsboard.org";
    User user = new User();
    user.setAuthority(Authority.TENANT_ADMIN);
    user.setTenantId(savedTenant.getId());
    user.setEmail(email);
    user.setFirstName("Joe");
    user.setLastName("Downs");

    User savedUser = createUserAndLogin(user, "testPassword1");
    logout();

    JsonNode resetPasswordByEmailRequest = new ObjectMapper().createObjectNode().put("email", email);

    doPost("/api/noauth/resetPasswordByEmail", resetPasswordByEmailRequest).andExpect(status().isOk());
    doGet("/api/noauth/resetPassword?resetToken={resetToken}", TestMailService.currentResetPasswordToken)
            .andExpect(status().isSeeOther()).andExpect(header().string(HttpHeaders.LOCATION,
                    "/login/resetPassword?resetToken=" + TestMailService.currentResetPasswordToken));

    JsonNode resetPasswordRequest = new ObjectMapper().createObjectNode()
            .put("resetToken", TestMailService.currentResetPasswordToken).put("password", "testPassword2");

    JsonNode tokenInfo = readResponse(
            doPost("/api/noauth/resetPassword", resetPasswordRequest).andExpect(status().isOk()),
            JsonNode.class);
    validateAndSetJwtToken(tokenInfo, email);

    doGet("/api/auth/user").andExpect(status().isOk())
            .andExpect(jsonPath("$.authority", is(Authority.TENANT_ADMIN.name())))
            .andExpect(jsonPath("$.email", is(email)));

    logout();

    login(email, "testPassword2");
    doGet("/api/auth/user").andExpect(status().isOk())
            .andExpect(jsonPath("$.authority", is(Authority.TENANT_ADMIN.name())))
            .andExpect(jsonPath("$.email", is(email)));

    loginSysAdmin();
    doDelete("/api/user/" + savedUser.getId().getId().toString()).andExpect(status().isOk());

    doDelete("/api/tenant/" + savedTenant.getId().getId().toString()).andExpect(status().isOk());
}

From source file:org.thingsboard.server.controller.UserControllerTest.java

@Test
public void testSaveUser() throws Exception {
    loginSysAdmin();//from   ww w  . j a  va 2 s . c  om

    Tenant tenant = new Tenant();
    tenant.setTitle("My tenant");
    Tenant savedTenant = doPost("/api/tenant", tenant, Tenant.class);
    Assert.assertNotNull(savedTenant);

    String email = "tenant2@thingsboard.org";
    User user = new User();
    user.setAuthority(Authority.TENANT_ADMIN);
    user.setTenantId(savedTenant.getId());
    user.setEmail(email);
    user.setFirstName("Joe");
    user.setLastName("Downs");
    User savedUser = doPost("/api/user", user, User.class);
    Assert.assertNotNull(savedUser);
    Assert.assertNotNull(savedUser.getId());
    Assert.assertTrue(savedUser.getCreatedTime() > 0);
    Assert.assertEquals(user.getEmail(), savedUser.getEmail());

    User foundUser = doGet("/api/user/" + savedUser.getId().getId().toString(), User.class);
    Assert.assertEquals(foundUser, savedUser);

    logout();
    doGet("/api/noauth/activate?activateToken={activateToken}", TestMailService.currentActivateToken)
            .andExpect(status().isPermanentRedirect()).andExpect(header().string(HttpHeaders.LOCATION,
                    "/login/createPassword?activateToken=" + TestMailService.currentActivateToken));

    JsonNode tokenInfo = readResponse(doPost("/api/noauth/activate", "activateToken",
            TestMailService.currentActivateToken, "password", "testPassword").andExpect(status().isOk()),
            JsonNode.class);
    validateAndSetJwtToken(tokenInfo, email);

    doGet("/api/auth/user").andExpect(status().isOk())
            .andExpect(jsonPath("$.authority", is(Authority.TENANT_ADMIN.name())))
            .andExpect(jsonPath("$.email", is(email)));

    logout();

    login(email, "testPassword");

    doGet("/api/auth/user").andExpect(status().isOk())
            .andExpect(jsonPath("$.authority", is(Authority.TENANT_ADMIN.name())))
            .andExpect(jsonPath("$.email", is(email)));

    loginSysAdmin();
    doDelete("/api/user/" + savedUser.getId().getId().toString()).andExpect(status().isOk());

    doDelete("/api/tenant/" + savedTenant.getId().getId().toString()).andExpect(status().isOk());
}

From source file:org.thingsboard.server.controller.UserControllerTest.java

@Test
public void testResetPassword() throws Exception {
    loginSysAdmin();// w  ww  . j  a v a2s . c o m

    Tenant tenant = new Tenant();
    tenant.setTitle("My tenant");
    Tenant savedTenant = doPost("/api/tenant", tenant, Tenant.class);
    Assert.assertNotNull(savedTenant);

    String email = "tenant2@thingsboard.org";
    User user = new User();
    user.setAuthority(Authority.TENANT_ADMIN);
    user.setTenantId(savedTenant.getId());
    user.setEmail(email);
    user.setFirstName("Joe");
    user.setLastName("Downs");

    User savedUser = createUserAndLogin(user, "testPassword1");
    logout();
    doPost("/api/noauth/resetPasswordByEmail", "email", email).andExpect(status().isOk());
    doGet("/api/noauth/resetPassword?resetToken={resetToken}", TestMailService.currentResetPasswordToken)
            .andExpect(status().isPermanentRedirect()).andExpect(header().string(HttpHeaders.LOCATION,
                    "/login/resetPassword?resetToken=" + TestMailService.currentResetPasswordToken));

    JsonNode tokenInfo = readResponse(doPost("/api/noauth/resetPassword", "resetToken",
            TestMailService.currentResetPasswordToken, "password", "testPassword2").andExpect(status().isOk()),
            JsonNode.class);
    validateAndSetJwtToken(tokenInfo, email);

    doGet("/api/auth/user").andExpect(status().isOk())
            .andExpect(jsonPath("$.authority", is(Authority.TENANT_ADMIN.name())))
            .andExpect(jsonPath("$.email", is(email)));

    logout();

    login(email, "testPassword2");
    doGet("/api/auth/user").andExpect(status().isOk())
            .andExpect(jsonPath("$.authority", is(Authority.TENANT_ADMIN.name())))
            .andExpect(jsonPath("$.email", is(email)));

    loginSysAdmin();
    doDelete("/api/user/" + savedUser.getId().getId().toString()).andExpect(status().isOk());

    doDelete("/api/tenant/" + savedTenant.getId().getId().toString()).andExpect(status().isOk());
}