Search in sources :

Example 1 with UrlHelper

use of org.apache.struts2.views.util.UrlHelper in project struts by apache.

the class JSPRuntime method handle.

public static void handle(String location, boolean flush) throws Exception {
    final HttpServletResponse response = ServletActionContext.getResponse();
    final HttpServletRequest request = ServletActionContext.getRequest();
    int i = location.indexOf("?");
    if (i > 0) {
        // extract params from the url and add them to the request
        final UrlHelper urlHelperGetInstance = ServletActionContext.getContext().getInstance(UrlHelper.class);
        final UrlHelper contextUrlHelper = (urlHelperGetInstance != null ? urlHelperGetInstance : (UrlHelper) ActionContext.getContext().get(StrutsConstants.STRUTS_URL_HELPER));
        final UrlHelper urlHelper = (contextUrlHelper != null ? contextUrlHelper : new DefaultUrlHelper());
        String query = location.substring(i + 1);
        Map<String, Object> queryParams = urlHelper.parseQueryString(query, true);
        if (queryParams != null && !queryParams.isEmpty()) {
            Map<String, Parameter> newParams = new HashMap<>();
            for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
                newParams.put(entry.getKey(), new Parameter.Request(entry.getKey(), entry.getValue()));
            }
            ActionContext.getContext().getParameters().appendAll(newParams);
        }
        location = location.substring(0, i);
    }
    Servlet servlet = servletCache.get(location);
    HttpJspPage page = (HttpJspPage) servlet;
    page._jspService(request, response);
    if (flush)
        response.flushBuffer();
}
Also used : DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) UrlHelper(org.apache.struts2.views.util.UrlHelper) HashMap(java.util.HashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpJspPage(javax.servlet.jsp.HttpJspPage) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) Parameter(org.apache.struts2.dispatcher.Parameter) Servlet(javax.servlet.Servlet) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with UrlHelper

use of org.apache.struts2.views.util.UrlHelper in project entando-core by entando.

the class FrontServletActionRedirectResultIntegrationTest method executeServlet.

private void executeServlet(String expectedProtocol, String expectedAuthority, String expectedPath) throws Exception {
    UrlHelper urlHelper = this.getContainerObject(UrlHelper.class);
    this.initAction("/do/Api/Service", "list");
    FrontServletActionRedirectResult servlet = new FrontServletActionRedirectResult();
    servlet.setActionName("details");
    servlet.setUrlHelper(urlHelper);
    servlet.execute(super.getActionContext().getActionInvocation());
    URL aURL = new URL(servlet.getLocation());
    assertEquals(expectedProtocol, aURL.getProtocol());
    assertEquals(expectedAuthority, aURL.getAuthority());
    assertEquals(expectedPath, aURL.getPath());
    List<NameValuePair> params = URLEncodedUtils.parse(aURL.getQuery(), Charset.forName("UTF-8"));
    for (NameValuePair param : params) {
        System.out.println(param.getName() + " : " + param.getValue());
        if (param.getName().equals("internalServletActionPath")) {
            assertEquals("/ExtStr2/do/Api/Service/details", param.getValue());
        } else if (param.getName().equals("internalServletFrameDest")) {
            assertEquals("0", param.getValue());
        } else {
            fail();
        }
    }
}
Also used : NameValuePair(org.apache.http.NameValuePair) UrlHelper(org.apache.struts2.views.util.UrlHelper) URL(java.net.URL)

Example 3 with UrlHelper

use of org.apache.struts2.views.util.UrlHelper in project struts by apache.

the class NotURLClassLoader method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    result = new EmbeddedJSPResult();
    request = EasyMock.createNiceMock(HttpServletRequest.class);
    response = new MockHttpServletResponse();
    context = new MockServletContext();
    config = new MockServletConfig(context);
    final Map params = new HashMap();
    HttpSession session = EasyMock.createNiceMock(HttpSession.class);
    EasyMock.replay(session);
    EasyMock.expect(request.getSession()).andReturn(session).anyTimes();
    EasyMock.expect(request.getParameterMap()).andReturn(params).anyTimes();
    EasyMock.expect(request.getParameter("username")).andAnswer(() -> ActionContext.getContext().getParameters().get("username").getValue());
    EasyMock.expect(request.getAttribute("something")).andReturn("somethingelse").anyTimes();
    EasyMock.replay(request);
    // mock value stack
    ValueStack valueStack = EasyMock.createNiceMock(ValueStack.class);
    EasyMock.expect(valueStack.getActionContext()).andReturn(ActionContext.getContext()).anyTimes();
    EasyMock.replay(valueStack);
    // mock converter
    XWorkConverter converter = new DummyConverter();
    DefaultFileManager fileManager = new DefaultFileManager();
    fileManager.setReloadingConfigs(false);
    // mock container
    Container container = EasyMock.createNiceMock(Container.class);
    EasyMock.expect(container.getInstance(XWorkConverter.class)).andReturn(converter).anyTimes();
    TextParser parser = new OgnlTextParser();
    EasyMock.expect(container.getInstance(TextParser.class)).andReturn(parser).anyTimes();
    EasyMock.expect(container.getInstanceNames(FileManager.class)).andReturn(new HashSet<>()).anyTimes();
    EasyMock.expect(container.getInstance(FileManager.class)).andReturn(fileManager).anyTimes();
    UrlHelper urlHelper = new DefaultUrlHelper();
    EasyMock.expect(container.getInstance(UrlHelper.class)).andReturn(urlHelper).anyTimes();
    FileManagerFactory fileManagerFactory = new DummyFileManagerFactory();
    EasyMock.expect(container.getInstance(FileManagerFactory.class)).andReturn(fileManagerFactory).anyTimes();
    EasyMock.replay(container);
    ActionContext.of(new HashMap<>()).withParameters(HttpParameters.create(params).build()).withServletRequest(request).withServletResponse(response).withServletContext(context).withContainer(container).withValueStack(valueStack).bind();
}
Also used : OgnlTextParser(com.opensymphony.xwork2.util.OgnlTextParser) FileManagerFactory(com.opensymphony.xwork2.FileManagerFactory) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) UrlHelper(org.apache.struts2.views.util.UrlHelper) ValueStack(com.opensymphony.xwork2.util.ValueStack) TextParser(com.opensymphony.xwork2.util.TextParser) OgnlTextParser(com.opensymphony.xwork2.util.OgnlTextParser) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) MockServletConfig(org.springframework.mock.web.MockServletConfig) MockServletContext(org.springframework.mock.web.MockServletContext) DefaultFileManager(com.opensymphony.xwork2.util.fs.DefaultFileManager) FileManager(com.opensymphony.xwork2.FileManager) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Container(com.opensymphony.xwork2.inject.Container) DefaultFileManager(com.opensymphony.xwork2.util.fs.DefaultFileManager) XWorkConverter(com.opensymphony.xwork2.conversion.impl.XWorkConverter) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HashSet(java.util.HashSet)

Aggregations

UrlHelper (org.apache.struts2.views.util.UrlHelper)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 DefaultUrlHelper (org.apache.struts2.views.util.DefaultUrlHelper)2 FileManager (com.opensymphony.xwork2.FileManager)1 FileManagerFactory (com.opensymphony.xwork2.FileManagerFactory)1 XWorkConverter (com.opensymphony.xwork2.conversion.impl.XWorkConverter)1 Container (com.opensymphony.xwork2.inject.Container)1 OgnlTextParser (com.opensymphony.xwork2.util.OgnlTextParser)1 TextParser (com.opensymphony.xwork2.util.TextParser)1 ValueStack (com.opensymphony.xwork2.util.ValueStack)1 DefaultFileManager (com.opensymphony.xwork2.util.fs.DefaultFileManager)1 URL (java.net.URL)1 HashSet (java.util.HashSet)1 Servlet (javax.servlet.Servlet)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 HttpJspPage (javax.servlet.jsp.HttpJspPage)1 NameValuePair (org.apache.http.NameValuePair)1