Skip to content

Commit

Permalink
Merge pull request #181 from caelum/ot-minorcodefixes
Browse files Browse the repository at this point in the history
Some code fixes, and removing deprecated elements
  • Loading branch information
garcia-jj committed Oct 7, 2013
2 parents 59088d5 + 7179fa3 commit 2284cba
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package br.com.caelum.vraptor.core;


import static java.util.Collections.unmodifiableMap;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -93,7 +95,7 @@ public boolean used() {

@Override
public Map<String, Object> included() {
return Collections.unmodifiableMap(includedAttributes);
return unmodifiableMap(includedAttributes);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ private String uriRelativeToContextRoot(HttpServletRequest request) {
}

private String removeQueryStringAndJSessionId(String uri) {
return uri.replaceAll("[\\?;].+", "");
if (uri.contains("?") || uri.contains(";")) {
return uri.replaceAll("[\\?;].+", "");
}
return uri;
}

private boolean isAFile(URL resourceUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

import br.com.caelum.vraptor.interceptor.InterceptorRegistry;

/**
* Cache all instances of {@link InterceptorHandler} when application starts.
*/
@ApplicationScoped
public class InterceptorStackHandlersCache {

Expand All @@ -42,7 +45,6 @@ public InterceptorStackHandlersCache() {}
@Inject
public InterceptorStackHandlersCache(InterceptorRegistry registry,
InterceptorHandlerFactory handlerFactory){

this.registry = registry;
this.handlerFactory = handlerFactory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package br.com.caelum.vraptor.core;

import static javax.servlet.RequestDispatcher.INCLUDE_REQUEST_URI;

import javax.enterprise.inject.Alternative;
import javax.inject.Inject;
import javax.servlet.FilterChain;
Expand All @@ -39,8 +41,6 @@ public class RequestInfo {
private final MutableResponse response;
private final FilterChain chain;

public static final String INCLUDE_REQUEST_URI = "javax.servlet.include.request_uri";

@Inject
public RequestInfo(ServletContext servletContext, FilterChain chain, MutableRequest request, MutableResponse response) {
this.servletContext = servletContext;
Expand Down Expand Up @@ -69,9 +69,7 @@ public String getRequestedUri() {
if (request.getAttribute(INCLUDE_REQUEST_URI) != null) {
return (String) request.getAttribute(INCLUDE_REQUEST_URI);
}
String uri = request.getRequestURI().replaceFirst("(?i);jsessionid=.*$", "");
String contextName = request.getContextPath();
uri = uri.replaceFirst(contextName, "");
return uri;
String uri = request.getRequestURI().substring(request.getContextPath().length());
return uri.replaceFirst("(?i);jsessionid=.*$", "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
* @author Caio Filipini
*/
public class ControllerNotFoundException extends RuntimeException {
/**
* Generated by Eclipse
*/
private static final long serialVersionUID = 121345908850681707L;

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
*/
public class IllegalRouteException extends VRaptorException {

/**
* Random
*/
private static final long serialVersionUID = -3854707059890751018L;

public IllegalRouteException(String msg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
*/
public class MethodNotAllowedException extends RuntimeException {

/**
* Generated by Eclipse
*/
private static final long serialVersionUID = 3864735692068670362L;
private final Set<HttpMethod> allowed ;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ protected String[] getURIsFor(Method javaMethod, Class<?> type) {
if (javaMethod.isAnnotationPresent(Path.class)) {
String[] uris = javaMethod.getAnnotation(Path.class).value();

checkArgument(uris.length > 0, "You must specify at least one path on @Path at " + javaMethod);
checkArgument(uris.length > 0, "You must specify at least one path on @Path at %s", javaMethod);
checkArgument(getUris(javaMethod).length == 0,
"You should specify paths either in @Path(\"/path\") or @Get(\"/path\") (or @Post, @Put, @Delete), not both at " + javaMethod);
"You should specify paths either in @Path(\"/path\") or @Get(\"/path\") (or @Post, @Put, @Delete), not both at %s", javaMethod);

fixURIs(type, uris);
return uris;
Expand Down Expand Up @@ -188,9 +188,7 @@ protected String removeTrailingSlash(String prefix) {
protected String extractPrefix(Class<?> type) {
if (type.isAnnotationPresent(Path.class)) {
String[] uris = type.getAnnotation(Path.class).value();
if (uris.length != 1) {
throw new IllegalArgumentException("You must specify exactly one path on @Path at " + type);
}
checkArgument(uris.length == 1, "You must specify exactly one path on @Path at %s", type);
return fixLeadingSlash(uris[0]);
} else {
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,10 @@ public <T extends View> T onErrorUse(Class<T> view) {
return new MockResult(proxifier).use(view); //ignore anything, no errors occurred
}

if (logger.isDebugEnabled()) {
logger.debug("there are errors on result: {}", errors);
}

result.include("errors", getErrors());
outjector.outjectRequestMap();


logger.debug("there are errors on result: {}", errors);
return viewsFactory.instanceFor(view, errors);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ public ReplicatorOutjector(Result result, MethodInfo method, ParameterNameProvid
}

public void outjectRequestMap() {
String[] names = provider.parameterNamesFor(method.getControllerMethod().getMethod());
for (int i = 0; i < names.length; i++) {
result.include(names[i], method.getParameters()[i]);
}
String[] names = provider.parameterNamesFor(method.getControllerMethod().getMethod());
for (int i = 0; i < names.length; i++) {
result.include(names[i], method.getParameters()[i]);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package br.com.caelum.vraptor.view;

import static com.google.common.base.Preconditions.checkArgument;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -130,17 +132,13 @@ public <T> T redirectTo(final Class<T> type) {

return proxifier.proxify(type, new MethodInvocation<T>() {
public Object intercept(T proxy, Method method, Object[] args, SuperMethod superMethod) {
if (!acceptsHttpGet(method)) {
throw new IllegalArgumentException(
"Your logic method must accept HTTP GET method if you want to redirect to it");
}
checkArgument(acceptsHttpGet(method), "Your logic method must accept HTTP GET method if you want to redirect to it");

try {
String path = request.getContextPath();
String url = router.urlFor(type, method, args);
String path = request.getContextPath() + url;
includeParametersInFlash(type, method, args);

path = path + url;

logger.debug("redirecting to {}", path);
response.sendRedirect(path);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public DefaultPageResult(MutableRequest req, MutableResponse res, MethodInfo req
this.resolver = resolver;
}

@Override
public void defaultView() {
String to = resolver.pathFor(requestInfo.getControllerMethod());
logger.debug("forwarding to {}", to);
Expand All @@ -74,6 +75,7 @@ public void defaultView() {
}
}

@Override
public void include() {
try {
String to = resolver.pathFor(requestInfo.getControllerMethod());
Expand All @@ -86,6 +88,7 @@ public void include() {
}
}

@Override
public void redirectTo(String url) {
logger.debug("redirection to {}", url);

Expand All @@ -100,6 +103,7 @@ public void redirectTo(String url) {
}
}

@Override
public void forwardTo(String url) {
logger.debug("forwarding to {}", url);

Expand All @@ -112,6 +116,7 @@ public void forwardTo(String url) {
}
}

@Override
public <T> T of(final Class<T> controllerType) {
return proxifier.proxify(controllerType, new MethodInvocation<T>() {
public Object intercept(T proxy, Method method, Object[] args, SuperMethod superMethod) {
Expand All @@ -127,17 +132,4 @@ public Object intercept(T proxy, Method method, Object[] args, SuperMethod super
});
}

public void redirect(String url) {
this.redirectTo(url);
}

public void forward(String url) {
this.forwardTo(url);
}

public void forward() {
this.defaultView();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@

import static br.com.caelum.vraptor.view.Results.logic;
import static br.com.caelum.vraptor.view.Results.page;
import static com.google.common.base.Preconditions.checkState;

import java.util.ArrayList;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;

import com.google.common.base.Preconditions;

import net.vidageek.mirror.dsl.Mirror;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.controller.ControllerMethod;
Expand Down Expand Up @@ -88,9 +91,7 @@ public void redirect() throws IllegalStateException {

private String getReferer() {
String referer = request.getHeader("Referer");
if (referer == null) {
throw new IllegalStateException("The Referer header was not specified");
}
checkState(referer != null, "The Referer header was not specified");

String path = request.getContextPath();
return referer.substring(referer.indexOf(path) + path.length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,4 @@ public interface PageResult extends View {
*/
<T> T of(Class<T> controllerType);

/**
* @deprecated As of 3.2, replaced by
* {@link #redirectTo(String url)}
*/
void redirect(String url);

/**
* @deprecated As of 3.2, replaced by
* {@link #forwardTo(String url)}
*/
void forward(String url);

/**
* @deprecated As of 3.2, replaced by
* {@link #forwardTo()}
*/
void forward();

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package br.com.caelum.vraptor.http;

import static javax.servlet.RequestDispatcher.INCLUDE_REQUEST_URI;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -64,7 +65,7 @@ public void setup() {
@Test
public void handlesInclude() {

when(request.getAttribute(RequestInfo.INCLUDE_REQUEST_URI)).thenReturn("/url");
when(request.getAttribute(INCLUDE_REQUEST_URI)).thenReturn("/url");
when(request.getMethod()).thenReturn("POST");
when(router.parse("/url", HttpMethod.POST, webRequest)).thenReturn(method);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,26 @@ public MockedPage() {
proxifier = new JavassistProxifier();
}

public void forwardTo() {

}

@Override
public void forwardTo(String url) {

}

@Override
public void defaultView() {

}

@Override
public void include() {
}

@Override
public void redirectTo(String url) {

}

@Override
public <T> T of(Class<T> controllerType) {
return proxifier.proxify(controllerType, new MethodInvocation<T>() {

Expand All @@ -62,19 +63,4 @@ public Object intercept(T proxy, Method method, Object[] args, SuperMethod super

});
}

public void redirect(String url) {
this.redirectTo(url);
}

public void forward(String url) {
this.forwardTo(url);
}

public void forward() {
this.defaultView();
}



}

0 comments on commit 2284cba

Please sign in to comment.