Skip to content
Snippets Groups Projects
Commit a7104f01 authored by Vincent Massol's avatar Vincent Massol
Browse files

XWIKI-13339: Introduce an Entity Reference Resolver/Serializer that can be...

XWIKI-13339: Introduce an Entity Reference Resolver/Serializer that can be used in URL parsing/generation
parent e2fd0aec
No related branches found
No related tags found
No related merge requests found
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.url.internal;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.xwiki.component.annotation.Component;
import org.xwiki.model.internal.reference.DefaultStringEntityReferenceResolver;
import org.xwiki.model.internal.reference.SymbolScheme;
/**
* Special resolver to resolve Entity References when they're used in URLs. This is needed for
* Tomcat since <a href="http://tomcat.apache.org/tomcat-7.0-doc/config/systemprops.html#Security">Tomcat has some
* default security settings preventing the usage of URL-encoded {@code /} and {@code \} in URLs</a> (to prevent
* Directory attacks.
*
* @version $Id$
* @since 8.1M2
*/
@Component
@Named("url")
@Singleton
public class URLStringEntityReferenceResolver extends DefaultStringEntityReferenceResolver
{
@Inject
@Named("url")
private SymbolScheme symbolScheme;
@Override
protected SymbolScheme getSymbolScheme()
{
return this.symbolScheme;
}
}
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.url.internal;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.xwiki.component.annotation.Component;
import org.xwiki.model.internal.reference.DefaultStringEntityReferenceSerializer;
import org.xwiki.model.internal.reference.SymbolScheme;
/**
* Special serializer to serialize Entity References when they're used in URLs. This is needed for
* Tomcat since <a href="http://tomcat.apache.org/tomcat-7.0-doc/config/systemprops.html#Security">Tomcat has some
* default security settings preventing the usage of URL-encoded {@code /} and {@code \} in URLs</a> (to prevent
* Directory attacks.
*
* @version $Id$
* @since 8.1M2
*/
@Component
@Named("url")
@Singleton
public class URLStringEntityReferenceSerializer extends DefaultStringEntityReferenceSerializer
{
@Inject
@Named("url")
private SymbolScheme symbolScheme;
@Override
protected SymbolScheme getSymbolScheme()
{
return this.symbolScheme;
}
}
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.url.internal;
import javax.inject.Named;
import javax.inject.Singleton;
import org.xwiki.component.annotation.Component;
import org.xwiki.model.internal.reference.DefaultSymbolScheme;
/**
* Special Symbol Scheme for serializing/resolving Entity Reference when they're used in URLs. This is needed for
* Tomcat since <a href="http://tomcat.apache.org/tomcat-7.0-doc/config/systemprops.html#Security">Tomcat has some
* default security settings preventing the usage of URL-encoded {@code /} and {@code \} in URLs</a> (to prevent
* Directory attacks.
*
* @version $Id$
* @since 8.1M2
*/
@Component
@Named("url")
@Singleton
public class URLSymbolScheme extends DefaultSymbolScheme
{
/**
* The escape char.
*/
private static final char CESCAPE = '!';
@Override
public Character getEscapeSymbol()
{
return CESCAPE;
}
}
......@@ -6,4 +6,7 @@ org.xwiki.url.internal.GenericResourceReferenceResolver
org.xwiki.url.internal.GenericStringResourceTypeResolver
org.xwiki.url.internal.DefaultStringResourceTypeResolver
org.xwiki.url.internal.URLExecutionContextInitializer
org.xwiki.url.internal.DefaultURLContextManager
\ No newline at end of file
org.xwiki.url.internal.DefaultURLContextManager
org.xwiki.url.internal.URLSymbolScheme
org.xwiki.url.internal.URLStringEntityReferenceSerializer
org.xwiki.url.internal.URLStringEntityReferenceResolver
\ No newline at end of file
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.url.internal;
import java.util.Arrays;
import org.junit.Rule;
import org.junit.Test;
import org.xwiki.model.EntityType;
import org.xwiki.model.internal.reference.DefaultSymbolScheme;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.test.annotation.ComponentList;
import org.xwiki.test.mockito.MockitoComponentMockingRule;
import static org.junit.Assert.*;
/**
* Unit tests for {@link URLStringEntityReferenceResolver}.
*
* @version $Id$
* @since 8.2M1
*/
@ComponentList({
DefaultSymbolScheme.class,
URLSymbolScheme.class
})
public class URLStringEntityReferenceResolverTest
{
@Rule
public MockitoComponentMockingRule<URLStringEntityReferenceResolver> mocker =
new MockitoComponentMockingRule<>(URLStringEntityReferenceResolver.class);
@Test
public void resolveReferences() throws Exception
{
EntityReference expected = new DocumentReference("wiki!", Arrays.asList("space:.!"), "page.!");
EntityReference reference = this.mocker.getComponentUnderTest().resolve("wiki!!:space!:!.!!.page!.!!",
EntityType.DOCUMENT);
assertEquals(expected, reference);
}
}
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.url.internal;
import java.util.Arrays;
import org.junit.Rule;
import org.junit.Test;
import org.xwiki.model.internal.reference.DefaultSymbolScheme;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.test.annotation.ComponentList;
import org.xwiki.test.mockito.MockitoComponentMockingRule;
import static org.junit.Assert.*;
/**
* Unit tests for {@link URLStringEntityReferenceSerializer}.
*
* @version $Id$
* @since 8.2M1
*/
@ComponentList({
DefaultSymbolScheme.class,
URLSymbolScheme.class
})
public class URLStringEntityReferenceSerializerTest
{
@Rule
public MockitoComponentMockingRule<URLStringEntityReferenceSerializer> mocker =
new MockitoComponentMockingRule<>(URLStringEntityReferenceSerializer.class);
@Test
public void serializeReferences() throws Exception
{
EntityReference reference = new DocumentReference("wiki!", Arrays.asList("space:.!"), "page.!");
assertEquals("wiki!!:space!:!.!!.page!.!!", this.mocker.getComponentUnderTest().serialize(reference));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment