3

I am trying to move redirect functionality from scriptlet to Sightly class. What I have done so far:

package apps.myproject.components.page.generic;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.wcm.foundation.ELEvaluator;
import com.adobe.cq.sightly.WCMUse;

public class PageComponent extends WCMUse {

    private static final Logger log = LoggerFactory.getLogger(PageComponent.class);

    public void activate() throws Exception {

    }

    public void redirect() throws IOException{
        String location = getProperties().get("redirectTarget", "");

        // resolve variables in path
        //where I can find PageContext Object?
        //location = ELEvaluator.evaluate(location, getRequest(), new PageContext());

        boolean wcmModeIsDisabled = getWcmMode().isDisabled();
        boolean wcmModeIsPreview = getWcmMode().isPreview();
        if ( (location.length() > 0) && ((wcmModeIsDisabled) || (wcmModeIsPreview)) ) {
            // check for recursion
            if (getCurrentPage() != null && !location.equals(getCurrentPage().getPath()) && location.length() > 0) {
                // check for absolute path
                final int protocolIndex = location.indexOf(":/");
                final int queryIndex = location.indexOf('?');
                final String redirectPath;
                if ( protocolIndex > -1 && (queryIndex == -1 || queryIndex > protocolIndex) ) {
                    redirectPath = location;
                } else {
                    redirectPath = getResourceResolver().map(getRequest(), location) + ".html";
                }
                getResponse().sendRedirect(redirectPath);
            } else {
                getResponse().sendError(HttpServletResponse.SC_NOT_FOUND);
            }
        }
    }
}

The problem is that I do not know where should I get the PageContext object for ELEvaluator.evaluate() method from. Passing null throws me NullPointerException, passing new PageContext() throws

java.lang.Error: Unresolved compilation problem: 
    Cannot instantiate the type PageContext

How to use ELEvaluator or something similar in Sightly class? Any ideas?

kmb
  • 871
  • 5
  • 17
  • 34
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Ashley Medway Feb 26 '15 at 16:37
  • 4
    @AshleyMedway No it is definitely not a duplicate. My question is how can I use ELEvaluator.evaluate() into Sightly class while I have no pageContext object over there? Optionally: is there something similar to ELEvaluator in Sightly that avoids using pageContext? Take a look at the last sentence of my question. – kmb Feb 26 '15 at 20:47
  • All null pointer exceptions are duplicates... – Ashley Medway Feb 26 '15 at 23:26
  • 3
    @AshleyMedway The question is not about the NullPointerException but about the usage of the particular method in a different way. – kmb Feb 27 '15 at 10:15

1 Answers1

0

Not really sure if it answers your question, but to me ELEvaluator.evaluate is to evaluate a potential expression stored in your redirectTarget property. If you're not looking for an edge case you can just ignore the whole line and focus on your redirect.

  • read the property
  • validate and externalize
  • response.sendRedirect(link)
cwoeltge
  • 201
  • 1
  • 4
  • Yes, the presented code works fine, it redirects me to a required location, but I'm still worried about the missing functionality of ELEvaluator. Thanks for your response. – kmb Feb 27 '15 at 12:39