org.eclipse.krazo.engine.Viewable Java Examples

The following examples show how to use org.eclipse.krazo.engine.Viewable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MvcViewExceptionMapper.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Override
public Response toResponse(MvcViewException exception) {

    ModelsImpl models = new ModelsImpl();
    models.put("error", exception.getMessage());
    Viewable viewable = new Viewable("mvc-error.jsp", models);

    return Response.status(492)
        .type(MediaType.TEXT_HTML_TYPE)
        .entity(viewable)
        .build();
}
 
Example #2
Source File: TaskController.java    From ee8-sandbox with Apache License 2.0 5 votes vote down vote up
@GET
@Path("{id}/edit")
public Viewable edit(@PathParam("id") Long id) {
    log.log(Level.INFO, "edit task @{0}", id);

    Task task = taskRepository.findById(id);

    models.put("task", task);
    return new Viewable("edit.jspx");
}
 
Example #3
Source File: TaskController.java    From ee8-sandbox with Apache License 2.0 5 votes vote down vote up
@GET
@Path("new")
public Viewable add() {
    log.log(Level.INFO, "add new task");

    return new Viewable("add.jspx");
}
 
Example #4
Source File: TaskController.java    From ee8-sandbox with Apache License 2.0 5 votes vote down vote up
@GET
@Path("{id}")
public Viewable taskDetails(@PathParam("id") @NotNull Long id) {
    log.log(Level.INFO, "get task by id@{0}", id);
    Task task = taskRepository.findById(id);

    models.put("details", task);
    return new Viewable("details.jspx");
}
 
Example #5
Source File: TaskController.java    From ee8-sandbox with Apache License 2.0 5 votes vote down vote up
@GET
@Path("{id}")
public Viewable taskDetails(@PathParam("id") @NotNull Long id) {
    log.log(Level.INFO, "get task by id@{0}", id);
    Task task = taskRepository.findById(id);

    models.put("details", task);
    return new Viewable("details.xhtml");
}
 
Example #6
Source File: ViewableWriterTest.java    From krazo with Apache License 2.0 5 votes vote down vote up
/**
 * Test isWriteable method.
 */
@Test
public void testIsWriteable() {
    ViewableWriter writer = new ViewableWriter();
    assertFalse(writer.isWriteable(null, null, new Annotation[] {}, MediaType.WILDCARD_TYPE));
    assertTrue(writer.isWriteable(Viewable.class, null, new Annotation[] {}, MediaType.WILDCARD_TYPE));
}
 
Example #7
Source File: TaskController.java    From ee8-sandbox with Apache License 2.0 5 votes vote down vote up
@GET
@Path("new")
public Viewable add() {
    log.log(Level.INFO, "add new task");

    return new Viewable("add.xhtml");
}
 
Example #8
Source File: TaskController.java    From ee8-sandbox with Apache License 2.0 5 votes vote down vote up
@GET
@Path("{id}/edit")
public Viewable edit(@PathParam("id") Long id) {
    log.log(Level.INFO, "edit task @{0}", id);

    Task task = taskRepository.findById(id);

    models.put("task", task);
    return new Viewable("edit.xhtml");
}
 
Example #9
Source File: EventDispatcher.java    From krazo with Apache License 2.0 5 votes vote down vote up
public void fireAfterProcessViewEvent(ViewEngine engine, Viewable viewable) {
    if (KrazoCdiExtension.isEventObserved(AfterProcessViewEvent.class)) {
        final AfterProcessViewEventImpl event = new AfterProcessViewEventImpl();
        event.setEngine(engine.getClass());
        event.setView(viewable.getView());
        mvcEventDispatcher.fire(event);
    }
}
 
Example #10
Source File: EventDispatcher.java    From krazo with Apache License 2.0 5 votes vote down vote up
public void fireBeforeProcessViewEvent(ViewEngine engine, Viewable viewable) {
    if (KrazoCdiExtension.isEventObserved(BeforeProcessViewEvent.class)) {
        final BeforeProcessViewEventImpl event = new BeforeProcessViewEventImpl();
        event.setEngine(engine.getClass());
        event.setView(viewable.getView());
        mvcEventDispatcher.fire(event);
    }
}
 
Example #11
Source File: HelloController.java    From krazo with Apache License 2.0 5 votes vote down vote up
/**
 * Sets locale to UK when using @View.
 */
@GET
@Path("locale2")
@Produces("text/html")      // overridden below
@View("hello.jsp")
public Response locale2() {
    return Response.ok(new Viewable("hello.jsp"), "application/xhtml+xml").language(Locale.UK).build();
}
 
Example #12
Source File: HelloController.java    From krazo with Apache License 2.0 5 votes vote down vote up
/**
 * Sets locale to UK.
 */
@GET
@Path("locale1")
@Produces("text/html")      // overridden below
public Response locale1() {
    return Response.ok(new Viewable("hello.jsp"), "application/xhtml+xml").language(Locale.UK).build();
}
 
Example #13
Source File: HelloController.java    From krazo with Apache License 2.0 5 votes vote down vote up
/**
 * Sets language to "es" when using @View.
 */
@GET
@Path("language2")
@Produces("text/html")      // overridden below
@View("hello.jsp")
public Response language2() {
    return Response.ok(new Viewable("hello.jsp"), "application/xhtml+xml").language("es").build();
}
 
Example #14
Source File: HelloController.java    From krazo with Apache License 2.0 5 votes vote down vote up
/**
 * Sets language to "es".
 */
@GET
@Path("language1")
@Produces("text/html")      // overridden below
public Response language1() {
    return Response.ok(new Viewable("hello.jsp"), "application/xhtml+xml").language("es").build();
}
 
Example #15
Source File: HelloController.java    From krazo with Apache License 2.0 5 votes vote down vote up
/**
 * XHTML type should override static HTML one in @Produces in this case
 * when using @View.
 */
@GET
@Path("other_produces2")
@Produces("text/html")      // overridden below
@View("hello.jsp")
public Response otherProduces2() {
    return Response.ok(new Viewable("hello.jsp"), "application/xhtml+xml").build();
}
 
Example #16
Source File: HelloController.java    From krazo with Apache License 2.0 5 votes vote down vote up
/**
 * XHTML type should override static HTML one in @Produces in this case.
 */
@GET
@Path("other_produces1")
@Produces("text/html")      // overridden below
public Response otherProduces1() {
    return Response.ok(new Viewable("hello.jsp"), "application/xhtml+xml").build();
}
 
Example #17
Source File: PersonController.java    From tomee with Apache License 2.0 5 votes vote down vote up
@GET
@Path("update/{id}")
public Viewable update(@PathParam("id") Long id) {

    Optional<Person> person = repository.findById(id);
    this.models.put("person", person.orElseThrow(NOT_FOUND_EXCEPTION));
    this.models.put("countries", getCountries());
    return new Viewable("change.jsp", models);
}
 
Example #18
Source File: PersonController.java    From tomee with Apache License 2.0 5 votes vote down vote up
@GET
@Path("update/{id}")
public Viewable update(@PathParam("id") Long id) {

    Optional<Person> person = repository.findById(id);
    this.models.put("person", person.orElseThrow(NOT_FOUND_EXCEPTION));
    this.models.put("countries", getCountries());
    return new Viewable("change.jsp", models);
}
 
Example #19
Source File: ViewableWriter.java    From krazo with Apache License 2.0 4 votes vote down vote up
@Override
public long getSize(Viewable viewable, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
    return -1;
}
 
Example #20
Source File: PersonController.java    From tomee with Apache License 2.0 4 votes vote down vote up
@GET
@Path("new")
public Viewable newElement() {
    this.models.put("countries", getCountries());
    return new Viewable("insert.jsp");
}
 
Example #21
Source File: PersonController.java    From tomee with Apache License 2.0 4 votes vote down vote up
@GET
@Path("new")
public Viewable newElement() {
    this.models.put("countries", getCountries());
    return new Viewable("insert.jsp");
}
 
Example #22
Source File: ViewableWriterTest.java    From krazo with Apache License 2.0 4 votes vote down vote up
/**
 * Test writeTo method.
 *
 * @throws Exception when a serious error occurs.
 */
@Test
public void testWriteTo() throws Exception {
    ViewableWriter writer = new ViewableWriter();

    Field mvcField = writer.getClass().getDeclaredField("mvc");
    mvcField.setAccessible(true);
    mvcField.set(writer, new MvcContextImpl());

    ViewEngineFinder finder = EasyMock.createStrictMock(ViewEngineFinder.class);
    Field finderField = writer.getClass().getDeclaredField("engineFinder");
    finderField.setAccessible(true);
    finderField.set(writer, finder);

    HttpServletRequest request = EasyMock.createStrictMock(HttpServletRequest.class);
    Field requestField = writer.getClass().getDeclaredField("injectedRequest");
    requestField.setAccessible(true);
    requestField.set(writer, request);

    Event<MvcEvent> dispatcher = EasyMock.createStrictMock(Event.class);
    Field dispatcherField = writer.getClass().getDeclaredField("dispatcher");
    dispatcherField.setAccessible(true);
    dispatcherField.set(writer, dispatcher);

    EventDispatcher eventDispatcher = EasyMock.createMock(EventDispatcher.class);
    Field eventDispatcherField = writer.getClass().getDeclaredField("eventDispatcher");
    eventDispatcherField.setAccessible(true);
    eventDispatcherField.set(writer, eventDispatcher);

    ViewEngine viewEngine = EasyMock.createStrictMock(ViewEngine.class);

    HttpServletResponse response = EasyMock.createStrictMock(HttpServletResponse.class);
    response.setContentType(eq("text/html;charset=UTF-8"));
    expect(response.getCharacterEncoding()).andReturn("UTF-8");
    Field responseField = writer.getClass().getDeclaredField("injectedResponse");
    responseField.setAccessible(true);
    responseField.set(writer, response);

    Configuration config = EasyMock.createStrictMock(Configuration.class);
    Field configField = writer.getClass().getDeclaredField("config");
    configField.setAccessible(true);
    configField.set(writer, config);

    MultivaluedHashMap map = new MultivaluedHashMap();
    ArrayList<MediaType> contentTypes = new ArrayList<>();
    contentTypes.add(MediaType.TEXT_HTML_TYPE);
    map.put("Content-Type", contentTypes);

    Viewable viewable = new Viewable("myview");
    viewable.setModels(new ModelsImpl());

    expect(finder.find(anyObject())).andReturn(viewEngine);
    viewEngine.processView((ViewEngineContext) anyObject());

    replay(finder, request, viewEngine, response);
    writer.writeTo(viewable, null, null, new Annotation[] {}, MediaType.TEXT_HTML_TYPE, map, null);
    verify(finder, request, viewEngine, response);
}
 
Example #23
Source File: ViewableWriter.java    From krazo with Apache License 2.0 4 votes vote down vote up
/**
 * Searches for a suitable {@link javax.mvc.engine.ViewEngine} to process the view. If no engine
 * is found, is forwards the request back to the servlet container.
 */
@Override
public void writeTo(Viewable viewable, Class<?> aClass, Type type, Annotation[] annotations, MediaType resolvedMediaType,
                    MultivaluedMap<String, Object> headers, OutputStream out)
    throws IOException, WebApplicationException {

    // Find engine for this Viewable
    final ViewEngine engine = engineFinder.find(viewable);
    if (engine == null) {
        throw new ServerErrorException(messages.get("NoViewEngine", viewable), INTERNAL_SERVER_ERROR);
    }

    // build the full media type (including the charset) and make sure the response header is set correctly
    MediaType mediaType = buildMediaTypeWithCharset(resolvedMediaType);
    headers.putSingle(HttpHeaders.CONTENT_TYPE, mediaType);

    HttpServletRequest request = unwrapOriginalRequest(injectedRequest);
    HttpServletResponse response = unwrapOriginalResponse(injectedResponse);

    // Create wrapper for response
    final ServletOutputStream responseStream = new DelegatingServletOutputStream(out);
    final HttpServletResponse responseWrapper = new MvcHttpServletResponse(response, responseStream, mediaType, headers);

    // Pass request to view engine
    try {
        // If no models in viewable, inject via CDI
        Models models = viewable.getModels();
        if (models == null) {
            models = modelsInstance.get();
        }

        // Bind EL 'mvc' object in models
        models.put("mvc", mvc);

        // Execute the view engine
        eventDispatcher.fireBeforeProcessViewEvent(engine, viewable);
        try {

            // Process view using selected engine
            engine.processView(new ViewEngineContextImpl(viewable.getView(), models, request, responseWrapper,
                                                         headers, responseStream, mediaType, uriInfo, resourceInfo, config, mvc.getLocale()));

        } finally {
            eventDispatcher.fireAfterProcessViewEvent(engine, viewable);
        }

    } catch (ViewEngineException e) {
        throw new ServerErrorException(INTERNAL_SERVER_ERROR, e);
    } finally {
        responseWrapper.getWriter().flush();
    }
}
 
Example #24
Source File: HelloController.java    From krazo with Apache License 2.0 4 votes vote down vote up
@Override
public Response toResponse(ClientErrorException exception) {
    return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
        .entity(new Viewable("hello.jsp"))
        .build();
}
 
Example #25
Source File: ViewableWriter.java    From krazo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isWriteable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
    return aClass == Viewable.class;
}
 
Example #26
Source File: DefaultExtensionController.java    From krazo with Apache License 2.0 4 votes vote down vote up
@GET
@Path("viewable")
public Viewable getViewAble() {
    return new Viewable("extension");
}
 
Example #27
Source File: HelloController.java    From krazo with Apache License 2.0 4 votes vote down vote up
@GET
@Path("response/viewable")
public Response getResponseViewable() {
    return Response.ok().entity(new Viewable("hello.jsp")).build();
}
 
Example #28
Source File: HelloController.java    From krazo with Apache License 2.0 4 votes vote down vote up
@GET
@Path("viewable")
public Viewable getViewable() {
    return new Viewable("hello.jsp");
}