ControllerUtils#getLocationWithoutParams does not cut parameters #PL-2644

This commit is contained in:
Yuriy Artamonov 2013-09-17 15:27:13 +00:00
parent 6a3250bb9d
commit 0ebdd35f0d
2 changed files with 33 additions and 2 deletions

View File

@ -11,6 +11,8 @@ import com.haulmont.cuba.security.app.LoginService;
import com.haulmont.cuba.security.global.UserSession;
import com.haulmont.cuba.web.App;
import com.haulmont.cuba.web.AppUI;
import com.vaadin.server.VaadinSession;
import com.vaadin.ui.UI;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -30,10 +32,14 @@ public abstract class ControllerUtils {
public static String getLocationWithoutParams() {
URI location = AppUI.getCurrent().getPage().getLocation();
return getLocationWithoutParams(location);
}
public static String getLocationWithoutParams(URI location) {
try {
StringBuilder baseUrl = new StringBuilder(location.toURL().toExternalForm());
if (location.getQuery() != null) {
baseUrl.delete(baseUrl.indexOf(location.getQuery()) - 1, baseUrl.length());
baseUrl.delete(baseUrl.indexOf("?" + location.getQuery()), baseUrl.length());
}
return baseUrl.toString();
} catch (MalformedURLException e) {
@ -103,4 +109,4 @@ public abstract class ControllerUtils {
return userSession;
}
}
}
}

View File

@ -0,0 +1,25 @@
/*
* Copyright (c) 2008-2013 Haulmont. All rights reserved.
* Use is subject to license terms, see http://www.cuba-platform.com/license for details.
*/
package com.haulmont.cuba.web.test;
import com.haulmont.cuba.web.controllers.ControllerUtils;
import junit.framework.TestCase;
import java.net.URI;
/**
* @author artamonov
* @version $Id$
*/
public class ControllerUtilsTest extends TestCase {
public void testGetLocationWithoutParams() throws Exception {
URI localUrl = new URI("http://localhost:8080/app?a");
assertEquals(ControllerUtils.getLocationWithoutParams(localUrl), "http://localhost:8080/app");
URI externalUrl = new URI("http://ya.ru/app/sample/?param=value");
assertEquals(ControllerUtils.getLocationWithoutParams(externalUrl), "http://ya.ru/app/sample/");
}
}