Search in sources :

Example 21 with Handle

use of org.apache.tapestry5.internal.plastic.asm.Handle in project tapestry-5 by apache.

the class TapestryModule method contributeComponentEventRequestHandler.

/**
 * Contributes filters:
 * <dl>
 * <dt>Ajax</dt>
 * <dd>Determines if the request is Ajax oriented, and redirects to an alternative handler if so</dd>
 * <dt>Secure</dt>
 * <dd>Sends a redirect if an non-secure request accesses a secure page</dd>
 * </dl>
 */
public void contributeComponentEventRequestHandler(OrderedConfiguration<ComponentEventRequestFilter> configuration, final RequestSecurityManager requestSecurityManager, @Ajax ComponentEventRequestHandler ajaxHandler) {
    ComponentEventRequestFilter secureFilter = new ComponentEventRequestFilter() {

        public void handle(ComponentEventRequestParameters parameters, ComponentEventRequestHandler handler) throws IOException {
            if (requestSecurityManager.checkForInsecureComponentEventRequest(parameters))
                return;
            handler.handle(parameters);
        }
    };
    configuration.add("Secure", secureFilter);
    configuration.add("Ajax", new AjaxFilter(request, ajaxHandler));
}
Also used : ComponentEventRequestFilter(org.apache.tapestry5.services.ComponentEventRequestFilter) ComponentEventRequestHandler(org.apache.tapestry5.services.ComponentEventRequestHandler) ComponentEventRequestParameters(org.apache.tapestry5.services.ComponentEventRequestParameters)

Example 22 with Handle

use of org.apache.tapestry5.internal.plastic.asm.Handle in project tapestry-5 by apache.

the class PageTester method pushFieldValuesIntoRequest.

private void pushFieldValuesIntoRequest(Element form) {
    Visitor visitor = new Visitor() {

        public void visit(Element element) {
            if (InternalUtils.isNonBlank(element.getAttribute("disabled")))
                return;
            String name = element.getName();
            if (name.equals("input")) {
                String type = extractNonBlank(element, "type");
                if (type.equals("radio") || type.equals("checkbox")) {
                    if (InternalUtils.isBlank(element.getAttribute("checked")))
                        return;
                }
                if (type.equals("button") || type.equals("submit"))
                    return;
                // Handle radio, checkbox, text, radio, hidden
                String value = element.getAttribute("value");
                if (InternalUtils.isNonBlank(value))
                    request.loadParameter(extractNonBlank(element, "name"), value);
                return;
            }
            if (name.equals("option")) {
                String value = element.getAttribute("value");
                if (InternalUtils.isNonBlank(element.getAttribute("selected"))) {
                    String selectName = extractNonBlank(findAncestor(element, "select"), "name");
                    request.loadParameter(selectName, value);
                }
                return;
            }
            if (name.equals("textarea")) {
                String content = element.getChildMarkup();
                if (InternalUtils.isNonBlank(content))
                    request.loadParameter(extractNonBlank(element, "name"), content);
                return;
            }
        }
    };
    form.visit(visitor);
}
Also used : Visitor(org.apache.tapestry5.dom.Visitor) Element(org.apache.tapestry5.dom.Element)

Example 23 with Handle

use of org.apache.tapestry5.internal.plastic.asm.Handle in project tapestry-5 by apache.

the class ActivationRequestParameterWorker method mapFieldToQueryParameter.

private void mapFieldToQueryParameter(PlasticField field, TransformationSupport support) {
    ActivationRequestParameter annotation = field.getAnnotation(ActivationRequestParameter.class);
    String parameterName = getParameterName(field, annotation);
    // Assumption: the field type is not one that's loaded by the component class loader, so it's safe
    // to convert to a hard type during class transformation.
    Class fieldType = classCache.forName(field.getTypeName());
    ValueEncoder encoder = valueEncoderSource.getValueEncoder(fieldType);
    FieldHandle handle = field.getHandle();
    String fieldName = String.format("%s.%s", field.getPlasticClass().getClassName(), field.getName());
    setValueFromInitializeEventHandler(support, fieldName, annotation.required(), handle, parameterName, encoder, urlEncoder);
    decorateLinks(support, fieldName, handle, parameterName, encoder, urlEncoder);
    preallocateName(support, parameterName);
}
Also used : ActivationRequestParameter(org.apache.tapestry5.annotations.ActivationRequestParameter) ValueEncoder(org.apache.tapestry5.ValueEncoder) PlasticClass(org.apache.tapestry5.plastic.PlasticClass) FieldHandle(org.apache.tapestry5.plastic.FieldHandle)

Example 24 with Handle

use of org.apache.tapestry5.internal.plastic.asm.Handle in project tapestry-5 by apache.

the class PageActivationContextWorker method createPassivateHandler.

private static ComponentEventHandler createPassivateHandler(final FieldHandle[] handles) {
    return new ComponentEventHandler() {

        public void handleEvent(Component instance, ComponentEvent event) {
            Object result;
            if (handles.length == 1) {
                // simple / common case for a single @PageActivationContext
                result = handles[0].get(instance);
            } else {
                LinkedList<Object> list = CollectionFactory.newLinkedList();
                // iterate backwards
                for (int i = handles.length - 1; i > -1; i--) {
                    FieldHandle handle = handles[i];
                    Object value = handle.get(instance);
                    // ignore trailing nulls
                    if (value != null || !list.isEmpty()) {
                        list.addFirst(value);
                    }
                }
                result = list.isEmpty() ? null : list;
            }
            event.storeResult(result);
        }
    };
}
Also used : ComponentEventHandler(org.apache.tapestry5.services.ComponentEventHandler) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Component(org.apache.tapestry5.runtime.Component) FieldHandle(org.apache.tapestry5.plastic.FieldHandle)

Example 25 with Handle

use of org.apache.tapestry5.internal.plastic.asm.Handle in project tapestry-5 by apache.

the class XMLTokenStreamTests method testStreamEncoding.

/**
 * This test sets the system's default encoding to cp1252 (as on german windows) and tries to parse a non-ascii xml file
 * with XMLTokenStream. This is to test a critical section within XMLTokenStream.openStream() where the
 * binary file is converted to charcters and back before it is parsed.
 * @throws Exception
 */
@Test
public void testStreamEncoding() throws Exception {
    String oldEncoding = System.getProperty("file.encoding");
    System.setProperty("file.encoding", "cp1252");
    resetDefaultCharset();
    try {
        MappedConfigurationStub<String, URL> parserUrlMap = new MappedConfigurationStub<String, URL>();
        String unicodeString = "\u00FC";
        String testDocument = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<root>" + unicodeString + "</root>\n";
        XMLTokenStream xts = new XMLTokenStream(new ResourceStub(testDocument.getBytes("utf-8")), parserUrlMap);
        // Ugly way to handle exceptions like java.io.IOException: Server returned HTTP response code: 503 for URL: http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent
        // when running tests
        int attempts = 0;
        int maxAttempts = 10;
        boolean success = false;
        while (!success && attempts < maxAttempts) {
            try {
                xts.parse();
            } catch (IOException e) {
                e.printStackTrace();
                Thread.sleep(5000);
                attempts++;
                continue;
            }
            success = true;
        }
        if (attempts == maxAttempts) {
            throw new RuntimeException("Maximum number of attempts reached");
        }
        Assert.assertEquals(xts.next(), XMLTokenType.START_ELEMENT);
        Assert.assertEquals(xts.getLocalName(), "root");
        Assert.assertEquals(xts.next(), XMLTokenType.CHARACTERS);
        Assert.assertEquals(xts.getText(), unicodeString);
        Assert.assertEquals(xts.next(), XMLTokenType.END_ELEMENT);
        Assert.assertEquals(xts.next(), XMLTokenType.END_DOCUMENT);
    } finally {
        System.setProperty("file.encoding", oldEncoding);
        resetDefaultCharset();
    }
}
Also used : IOException(java.io.IOException) XMLTokenStream(org.apache.tapestry5.internal.services.XMLTokenStream) URL(java.net.URL) Test(org.testng.annotations.Test)

Aggregations

Component (org.apache.tapestry5.runtime.Component)5 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)4 ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)4 ComponentEventHandler (org.apache.tapestry5.services.ComponentEventHandler)4 TrackableComponentEventCallback (org.apache.tapestry5.TrackableComponentEventCallback)3 ConstantDynamic (org.apache.tapestry5.internal.plastic.asm.ConstantDynamic)3 Handle (org.apache.tapestry5.internal.plastic.asm.Handle)3 Type (org.apache.tapestry5.internal.plastic.asm.Type)3 Page (org.apache.tapestry5.internal.structure.Page)3 FieldHandle (org.apache.tapestry5.plastic.FieldHandle)3 ComponentEventRequestHandler (org.apache.tapestry5.services.ComponentEventRequestHandler)3 IOException (java.io.IOException)2 Link (org.apache.tapestry5.http.Link)2 ComponentPageElement (org.apache.tapestry5.internal.structure.ComponentPageElement)2 Contribute (org.apache.tapestry5.ioc.annotations.Contribute)2 JSONObject (org.apache.tapestry5.json.JSONObject)2 ComponentEventRequestFilter (org.apache.tapestry5.services.ComponentEventRequestFilter)2 PageRenderRequestFilter (org.apache.tapestry5.services.PageRenderRequestFilter)2 PageRenderRequestHandler (org.apache.tapestry5.services.PageRenderRequestHandler)2 Test (org.testng.annotations.Test)2