Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip context path correctly if deployed to the root context #731

Merged
merged 3 commits into from
Aug 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.com.caelum.vraptor.http.VRaptorRequest;

/**
* Handles default content if the request corresponds to static content.
*
Expand Down Expand Up @@ -63,10 +65,10 @@ public boolean requestingStaticFile(HttpServletRequest request) throws Malformed
}

private String uriRelativeToContextRoot(HttpServletRequest request) {
String uri = request.getRequestURI().substring(request.getContextPath().length());
String uri = VRaptorRequest.getRelativeRequestURI(request);
return removeQueryStringAndJSessionId(uri);
}

private String removeQueryStringAndJSessionId(String uri) {
if (uri.contains("?") || uri.contains(";")) {
return uri.replaceAll("[\\?;].+", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,17 @@ public String getRequestedUri() {
if (getAttribute(INCLUDE_REQUEST_URI) != null) {
return (String) getAttribute(INCLUDE_REQUEST_URI);
}
String uri = getRequestURI().substring(getContextPath().length());
String uri = getRelativeRequestURI(this);
return uri.replaceFirst("(?i);jsessionid=.*$", "");
}

public static String getRelativeRequestURI(HttpServletRequest request) {
if ("/".equals(request.getContextPath())) {
return request.getRequestURI();
}
return request.getRequestURI().substring(request.getContextPath().length());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should have here a method, on VRaptorRequest/MutableRequest, returning the request uri without the context path, so we don't duplicate this logic here and on DefaultStaticContentHandler. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But in that case we would need to cast that HttpServletRequest in DefaultStaticContentHandler, this could be problematic, right? But I agree that we should'nt duplicate this code, maybe we should create a component with this logic and inject it in both classes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, not a good solution =/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

big enough for a component?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its kind of overkill :-\

maybe we should leave as it is? create a static method (urgh)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the duplication =/ I prefer the static method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine for me :D
@chkal, what do you think? do you want to refactor that code extracting a static method and reuse it in both classes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm on my phone and on vacation right now, so this will be short...

I had this issue with Undertow, which is the Servlet container of Wildfly.

And yeah, sure, I can refactor the patch. Creating a single static method
for this makes sense. I'll do this next week after returning from vacation.
Am 06.08.2014 21:17 schrieb "csokol" notifications@github.com:

In
vraptor-core/src/main/java/br/com/caelum/vraptor/http/VRaptorRequest.java:

    return uri.replaceFirst("(?i);jsessionid=.*$", "");
}
  • private String stripContextPath(String uri) {
  •   if ("/".equals(getContextPath())) {
    
  •       return uri;
    
  •   }
    
  •   return uri.substring(getContextPath().length());
    
  • }

Fine for me :D
@chkal https://github.com/chkal, what do you think? do you want to
refactor that code extracting a static method and reuse it in both classes?

Reply to this email directly or view it on GitHub
https://github.com/caelum/vraptor4/pull/731/files#r15896613.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do this next week after returning from vacation.

thanks @chkal, enjoy your vacation's last week, we can wait for it :)


@Override
public String toString() {
return String.format("[VRaptorRequest %s]", this.getRequest());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ public void supportsGreaterLengthArrays() {
assertThat(vraptor.getParameterValues("name"), is(equalTo(values)));
}

@Test
public void strippsContextPath() {
when(request.getContextPath()).thenReturn("/myapp");
when(request.getRequestURI()).thenReturn("/myapp/somepage");
assertThat(vraptor.getRequestedUri(), is(equalTo("/somepage")));
}

@Test
public void strippsRootContextPath() {
when(request.getContextPath()).thenReturn("/");
when(request.getRequestURI()).thenReturn("/somepage");
assertThat(vraptor.getRequestedUri(), is(equalTo("/somepage")));
}

@Test
public void returnsAllNamesOnlyOnce() {
vraptor.setParameter("name", "silveira");
Expand Down