Search in sources :

Example 6 with Response

use of org.apache.tapestry5.http.services.Response in project tapestry-5 by apache.

the class ComponentEventDispatcherTest method test.

private void test(String requestPath, String localeName, String containerPageName, String nestedComponentId, String eventType, String... eventContext) throws IOException {
    ComponentRequestHandler handler = mockComponentRequestHandler();
    Request request = mockRequest();
    Response response = mockResponse();
    ComponentClassResolver resolver = mockComponentClassResolver();
    LocalizationSetter localizationSetter = mockLocalizationSetter();
    MetaDataLocator metaDataLocator = neverWhitelistProtected();
    ComponentEventRequestParameters expectedParameters = new ComponentEventRequestParameters(containerPageName, containerPageName, nestedComponentId, eventType, new EmptyEventContext(), new URLEventContext(contextValueEncoder, eventContext));
    train_getPath(request, requestPath);
    expect(localizationSetter.isSupportedLocaleName(localeName)).andReturn(false);
    train_isPageName(resolver, containerPageName, true);
    train_canonicalizePageName(resolver, containerPageName, containerPageName);
    train_getParameter(request, InternalConstants.PAGE_CONTEXT_NAME, null);
    train_getParameter(request, InternalConstants.CONTAINER_PAGE_NAME, null);
    expect(request.getAttribute(InternalConstants.REFERENCED_COMPONENT_NOT_FOUND)).andStubReturn(null);
    handler.handleComponentEvent(expectedParameters);
    train_for_request_locale(request, localizationSetter);
    replay();
    Dispatcher dispatcher = new ComponentEventDispatcher(handler, new ComponentEventLinkEncoderImpl(resolver, contextPathEncoder, localizationSetter, response, null, null, null, true, null, "", metaDataLocator, null));
    assertTrue(dispatcher.dispatch(request, response));
    verify();
}
Also used : Response(org.apache.tapestry5.http.services.Response) URLEventContext(org.apache.tapestry5.internal.URLEventContext) ComponentEventRequestParameters(org.apache.tapestry5.services.ComponentEventRequestParameters) EmptyEventContext(org.apache.tapestry5.internal.EmptyEventContext) Request(org.apache.tapestry5.http.services.Request) ComponentClassResolver(org.apache.tapestry5.services.ComponentClassResolver) ComponentRequestHandler(org.apache.tapestry5.services.ComponentRequestHandler) LocalizationSetter(org.apache.tapestry5.services.LocalizationSetter) Dispatcher(org.apache.tapestry5.http.services.Dispatcher) MetaDataLocator(org.apache.tapestry5.services.MetaDataLocator)

Example 7 with Response

use of org.apache.tapestry5.http.services.Response in project tapestry-5 by apache.

the class ComponentEventDispatcherTest method different_active_and_containing_pages.

@Test
public void different_active_and_containing_pages() throws Exception {
    ComponentRequestHandler handler = mockComponentRequestHandler();
    Request request = mockRequest();
    Response response = mockResponse();
    ComponentClassResolver resolver = mockComponentClassResolver();
    LocalizationSetter ls = mockLocalizationSetter();
    MetaDataLocator metaDataLocator = neverWhitelistProtected();
    ComponentEventRequestParameters expectedParameters = new ComponentEventRequestParameters("activepage", "mypage", "", "eventname", new EmptyEventContext(), new EmptyEventContext());
    train_getPath(request, "/activepage:eventname");
    expect(ls.isSupportedLocaleName("activepage:eventname")).andReturn(false);
    train_isPageName(resolver, "activepage", true);
    train_canonicalizePageName(resolver, "activepage", "activepage");
    train_getParameter(request, InternalConstants.PAGE_CONTEXT_NAME, null);
    train_getParameter(request, InternalConstants.CONTAINER_PAGE_NAME, "mypage");
    expect(request.getAttribute(InternalConstants.REFERENCED_COMPONENT_NOT_FOUND)).andStubReturn(null);
    train_canonicalizePageName(resolver, "mypage", "mypage");
    train_for_request_locale(request, ls);
    handler.handleComponentEvent(expectedParameters);
    replay();
    Dispatcher dispatcher = new ComponentEventDispatcher(handler, new ComponentEventLinkEncoderImpl(resolver, contextPathEncoder, ls, response, null, null, null, true, null, "", metaDataLocator, null));
    assertTrue(dispatcher.dispatch(request, response));
    verify();
}
Also used : Response(org.apache.tapestry5.http.services.Response) ComponentEventRequestParameters(org.apache.tapestry5.services.ComponentEventRequestParameters) EmptyEventContext(org.apache.tapestry5.internal.EmptyEventContext) Request(org.apache.tapestry5.http.services.Request) ComponentClassResolver(org.apache.tapestry5.services.ComponentClassResolver) ComponentRequestHandler(org.apache.tapestry5.services.ComponentRequestHandler) LocalizationSetter(org.apache.tapestry5.services.LocalizationSetter) Dispatcher(org.apache.tapestry5.http.services.Dispatcher) MetaDataLocator(org.apache.tapestry5.services.MetaDataLocator) Test(org.testng.annotations.Test)

Example 8 with Response

use of org.apache.tapestry5.http.services.Response in project tapestry-5 by apache.

the class MultipartServletRequestFilterTest method multipartRequestIsDecoded.

@Test
public void multipartRequestIsDecoded() throws Exception {
    MultipartDecoder decoder = newMock(MultipartDecoder.class);
    HttpServletRequest request = mockHttpServletRequest();
    HttpServletRequest decodedRequest = mockHttpServletRequest();
    HttpServletResponse response = mockHttpServletResponse();
    HttpServletRequestHandler handler = newMock(HttpServletRequestHandler.class);
    MultipartServletRequestFilter filter = new MultipartServletRequestFilter(decoder);
    expect(request.getMethod()).andReturn("post");
    expect(request.getContentType()).andReturn("multipart/form");
    expect(decoder.decode(request)).andReturn(decodedRequest);
    expect(handler.service(decodedRequest, response)).andReturn(true);
    replay();
    boolean isHandled = filter.service(request, response, handler);
    assertTrue(isHandled);
    verify();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MultipartDecoder(org.apache.tapestry5.upload.services.MultipartDecoder) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpServletRequestHandler(org.apache.tapestry5.http.services.HttpServletRequestHandler) Test(org.testng.annotations.Test)

Example 9 with Response

use of org.apache.tapestry5.http.services.Response in project tapestry-5 by apache.

the class TypeCoercerImpl method findOrCreateCoercion.

/**
 * Here's the real meat; we do a search of the space to find coercions, or a system of
 * coercions, that accomplish
 * the desired coercion.
 *
 * There's <strong>TREMENDOUS</strong> room to improve this algorithm. For example, inheritance lists could be
 * cached. Further, there's probably more ways to early prune the search. However, even with dozens or perhaps
 * hundreds of tuples, I suspect the search will still grind to a conclusion quickly.
 *
 * The order of operations should help ensure that the most efficient tuple chain is located. If you think about how
 * tuples are added to the queue, there are two factors: size (the number of steps in the coercion) and
 * "class distance" (that is, number of steps up the inheritance hiearchy). All the appropriate 1 step coercions
 * will be considered first, in class distance order. Along the way, we'll queue up all the 2 step coercions, again
 * in class distance order. By the time we reach some of those, we'll have begun queueing up the 3 step coercions, and
 * so forth, until we run out of input tuples we can use to fabricate multi-step compound coercions, or reach a
 * final response.
 *
 * This does create a good number of short lived temporary objects (the compound tuples), but that's what the GC is
 * really good at.
 *
 * @param sourceType
 * @param targetType
 * @return coercer from sourceType to targetType
 */
@SuppressWarnings("unchecked")
private Coercion findOrCreateCoercion(Class sourceType, Class targetType) {
    if (sourceType == Void.class) {
        return searchForNullCoercion(targetType);
    }
    // Trying to find exact match.
    Optional<CoercionTuple> maybeTuple = getTuples(sourceType, targetType).stream().filter((t) -> sourceType.equals(t.getSourceType()) && targetType.equals(t.getTargetType())).findFirst();
    if (maybeTuple.isPresent()) {
        return maybeTuple.get().getCoercion();
    }
    // These are instance variables because this method may be called concurrently.
    // On a true race, we may go to the work of seeking out and/or fabricating
    // a tuple twice, but it's more likely that different threads are looking
    // for different source/target coercions.
    Set<CoercionTuple.Key> consideredTuples = CollectionFactory.newSet();
    LinkedList<CoercionTuple> queue = CollectionFactory.newLinkedList();
    seedQueue(sourceType, targetType, consideredTuples, queue);
    while (!queue.isEmpty()) {
        CoercionTuple tuple = queue.removeFirst();
        // If the tuple results in a value type that is assignable to the desired target type,
        // we're done! Later, we may add a concept of "cost" (i.e. number of steps) or
        // "quality" (how close is the tuple target type to the desired target type). Cost
        // is currently implicit, as compound tuples are stored deeper in the queue,
        // so simpler coercions will be located earlier.
        Class tupleTargetType = tuple.getTargetType();
        if (targetType.isAssignableFrom(tupleTargetType)) {
            return tuple.getCoercion();
        }
        // So .. this tuple doesn't get us directly to the target type.
        // However, it *may* get us part of the way. Each of these
        // represents a coercion from the source type to an intermediate type.
        // Now we're going to look for conversions from the intermediate type
        // to some other type.
        queueIntermediates(sourceType, targetType, tuple, consideredTuples, queue);
    }
    throw new CoercionNotFoundException(String.format("Could not find a coercion from type %s to type %s.", sourceType.getName(), targetType.getName()), buildCoercionCatalog(), sourceType, targetType);
}
Also used : PlasticUtils(org.apache.tapestry5.plastic.PlasticUtils) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) InternalCommonsUtils(org.apache.tapestry5.commons.internal.util.InternalCommonsUtils) Collection(java.util.Collection) Set(java.util.Set) StringToEnumCoercion(org.apache.tapestry5.commons.util.StringToEnumCoercion) LockSupport(org.apache.tapestry5.commons.internal.util.LockSupport) List(java.util.List) F(org.apache.tapestry5.func.F) Coercion(org.apache.tapestry5.commons.services.Coercion) CoercionNotFoundException(org.apache.tapestry5.commons.util.CoercionNotFoundException) CollectionFactory(org.apache.tapestry5.commons.util.CollectionFactory) Map(java.util.Map) CoercionTuple(org.apache.tapestry5.commons.services.CoercionTuple) AvailableValues(org.apache.tapestry5.commons.util.AvailableValues) Optional(java.util.Optional) InheritanceSearch(org.apache.tapestry5.commons.internal.util.InheritanceSearch) LinkedList(java.util.LinkedList) Collections(java.util.Collections) WeakHashMap(java.util.WeakHashMap) CoercionFailedException(org.apache.tapestry5.commons.util.CoercionFailedException) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) CoercionNotFoundException(org.apache.tapestry5.commons.util.CoercionNotFoundException) CoercionTuple(org.apache.tapestry5.commons.services.CoercionTuple)

Example 10 with Response

use of org.apache.tapestry5.http.services.Response in project tapestry-5 by apache.

the class AjaxComponentInstanceEventResultProcessor method processResultValue.

public void processResultValue(Component value) throws IOException {
    ComponentResources resources = value.getComponentResources();
    boolean isPage = value == resources.getPage();
    String pageName = resources.getPageName();
    if (isPage) {
        // This will ultimately send a JSON response to redirect to the page
        masterProcessor.processResultValue(pageName);
        return;
    }
    // Otherwise, a component within a page. Components are transformed to implement RenderCommand, but if we just
    // pass the component itself to the master processor, we'll get in a loop, so we instead
    // pass the ComponentPageElement (which implements RenderCommand as well).
    Page page = cache.get(pageName);
    String nestedId = resources.getNestedId();
    RenderCommand command = page.getComponentElementByNestedId(nestedId);
    masterProcessor.processResultValue(command);
}
Also used : RenderCommand(org.apache.tapestry5.runtime.RenderCommand) Page(org.apache.tapestry5.internal.structure.Page) ComponentResources(org.apache.tapestry5.ComponentResources)

Aggregations

Test (org.testng.annotations.Test)53 Response (org.apache.tapestry5.http.services.Response)47 Request (org.apache.tapestry5.http.services.Request)25 Link (org.apache.tapestry5.http.Link)23 HttpServletResponse (javax.servlet.http.HttpServletResponse)16 LocalizationSetter (org.apache.tapestry5.services.LocalizationSetter)12 MetaDataLocator (org.apache.tapestry5.services.MetaDataLocator)12 ComponentClassResolver (org.apache.tapestry5.services.ComponentClassResolver)11 PageRenderRequestParameters (org.apache.tapestry5.services.PageRenderRequestParameters)11 EmptyEventContext (org.apache.tapestry5.internal.EmptyEventContext)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 RequestFilter (org.apache.tapestry5.http.services.RequestFilter)8 RequestHandler (org.apache.tapestry5.http.services.RequestHandler)8 JSONObject (org.apache.tapestry5.json.JSONObject)8 Context (org.apache.tapestry5.http.services.Context)7 Dispatcher (org.apache.tapestry5.http.services.Dispatcher)7 ComponentEventLinkEncoder (org.apache.tapestry5.services.ComponentEventLinkEncoder)7 ComponentEventRequestParameters (org.apache.tapestry5.services.ComponentEventRequestParameters)6 ComponentRequestHandler (org.apache.tapestry5.services.ComponentRequestHandler)6 IOException (java.io.IOException)5