Search in sources :

Example 26 with ResponseBuilder

use of jakarta.ws.rs.core.Response.ResponseBuilder in project jaxrs-api by eclipse-ee4j.

the class Resource method genericResponse.

@GET
@Path("{id}")
public Response genericResponse(@PathParam("id") String path) {
    ContextOperation op = ContextOperation.valueOf(path.toUpperCase());
    ResponseBuilder builder = createResponseBuilderWithHeader(op);
    switch(op) {
        case GETHEADERS:
            for (int i = 0; i != 5; i++) builder = builder.header(TemplateInterceptorBody.PROPERTY + i, "any");
            break;
        case PROCEEDTHROWSWEBAPPEXCEPTION:
            builder.entity(new OnWriteExceptionThrowingStringBean(TemplateInterceptorBody.ENTITY));
            break;
        default:
            break;
    }
    Response response = builder.build();
    return response;
}
Also used : Response(jakarta.ws.rs.core.Response) ContextOperation(ee.jakarta.tck.ws.rs.ee.rs.ext.interceptor.writer.writerinterceptorcontext.ContextOperation) OnWriteExceptionThrowingStringBean(ee.jakarta.tck.ws.rs.ee.rs.ext.interceptor.writer.writerinterceptorcontext.OnWriteExceptionThrowingStringBean) ResponseBuilder(jakarta.ws.rs.core.Response.ResponseBuilder) Path(jakarta.ws.rs.Path) GET(jakarta.ws.rs.GET)

Example 27 with ResponseBuilder

use of jakarta.ws.rs.core.Response.ResponseBuilder in project jaxrs-api by eclipse-ee4j.

the class Resource method buildResponse.

private Response buildResponse(Object content, MediaType type) {
    List<String> list = headers.getRequestHeader(HEADERNAME);
    String name = null;
    if (list != null && list.size() != 0)
        name = list.iterator().next();
    ResponseBuilder builder = Response.ok(content, type).type(type);
    if (name != null)
        builder.header(HEADERNAME, name);
    return builder.build();
}
Also used : ResponseBuilder(jakarta.ws.rs.core.Response.ResponseBuilder)

Example 28 with ResponseBuilder

use of jakarta.ws.rs.core.Response.ResponseBuilder in project jaxrs-api by eclipse-ee4j.

the class BuilderClientIT method expiresTest.

/*
   * @testName: expiresTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:147;
   * 
   * @test_Strategy: Set Expires to ResponseBuilder, build a response and check
   * expires timestamp.
   */
@Test
public void expiresTest() throws Fault {
    Date now = Calendar.getInstance().getTime();
    ResponseBuilder rs = Response.ok();
    rs.expires(now);
    Response response = rs.build();
    MultivaluedMap<String, Object> metadata = response.getMetadata();
    if (metadata == null)
        fail("No metadata in response");
    List<Object> expires = response.getMetadata().get("Expires");
    if (expires == null || expires.isEmpty())
        fail("No Expires property in metadata");
    boolean condition = false;
    Object fetched = expires.iterator().next();
    if (Date.class.isInstance(fetched))
        condition = ((Date) fetched).compareTo(now) == 0;
    else if (String.class.isInstance(fetched))
        condition = formats(now).contains(fetched.toString());
    else
        fail("Fetched object not recognised");
    assertTrue(condition, "Expires value not matched, set: " + now.toString() + "fetched:" + fetched.toString());
    logMsg("Set and fetched expire dates matched");
}
Also used : Response(jakarta.ws.rs.core.Response) ResponseBuilder(jakarta.ws.rs.core.Response.ResponseBuilder) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 29 with ResponseBuilder

use of jakarta.ws.rs.core.Response.ResponseBuilder in project jaxrs-api by eclipse-ee4j.

the class BuilderClientIT method allowStringSetTest.

/*
   * @testName: allowStringSetTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:876;
   * 
   * @test_Strategy: Set the list of allowed methods for the resource.
   */
@Test
public void allowStringSetTest() throws Fault {
    Set<String> methods = new TreeSet<String>();
    methods.add(Request.OPTIONS.name());
    methods.add(Request.TRACE.name());
    ResponseBuilder rb = RuntimeDelegate.getInstance().createResponseBuilder();
    Response response = rb.allow(methods).build();
    Set<String> set = response.getAllowedMethods();
    String responseMethods = JaxrsUtil.iterableToString(" ", set);
    for (String method : methods) {
        assertContains(responseMethods, method, "Expected allow method", method, "was not found in response allowed methods", responseMethods);
        logMsg("Found expected allowed method", method);
    }
}
Also used : Response(jakarta.ws.rs.core.Response) TreeSet(java.util.TreeSet) ResponseBuilder(jakarta.ws.rs.core.Response.ResponseBuilder) Test(org.junit.jupiter.api.Test)

Example 30 with ResponseBuilder

use of jakarta.ws.rs.core.Response.ResponseBuilder in project jaxrs-api by eclipse-ee4j.

the class Resource method getLastModified.

@POST
@Path("getlastmodified")
public Response getLastModified(String last) {
    ResponseBuilder builder = createResponseWithHeader();
    if (last != null && last.length() != 0) {
        long lastMilist = Long.parseLong(last);
        Date date = new Date(lastMilist);
        builder = builder.lastModified(date);
    }
    Response response = builder.build();
    return response;
}
Also used : Response(jakarta.ws.rs.core.Response) ResponseBuilder(jakarta.ws.rs.core.Response.ResponseBuilder) Date(java.util.Date) Path(jakarta.ws.rs.Path) POST(jakarta.ws.rs.POST)

Aggregations

ResponseBuilder (jakarta.ws.rs.core.Response.ResponseBuilder)80 Response (jakarta.ws.rs.core.Response)69 Path (jakarta.ws.rs.Path)41 Test (org.junit.jupiter.api.Test)34 POST (jakarta.ws.rs.POST)27 GET (jakarta.ws.rs.GET)14 Date (java.util.Date)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Annotation (java.lang.annotation.Annotation)4 VerificationResult (ee.jakarta.tck.ws.rs.api.rs.core.responseclient.VerificationResult)3 StringBean (ee.jakarta.tck.ws.rs.common.provider.StringBean)3 NewCookie (jakarta.ws.rs.core.NewCookie)3 RuntimeDelegate (jakarta.ws.rs.ext.RuntimeDelegate)3 IOException (java.io.IOException)3 StringBeanWithAnnotation (ee.jakarta.tck.ws.rs.common.provider.StringBeanWithAnnotation)2 Produces (jakarta.ws.rs.Produces)2 WebApplicationException (jakarta.ws.rs.WebApplicationException)2 EntityTag (jakarta.ws.rs.core.EntityTag)2 Link (jakarta.ws.rs.core.Link)2 InputStreamReader (java.io.InputStreamReader)2