Projects >> wro4j >>2b86402426711e61042c16af2497ab6fca99010b

Chunk
* * * * * * *
<<<<<<< HEAD /* * Copyright (c) 2008. All rights reserved. */ package ro.isdc.wro.model.resource.processor.impl.css; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ro.isdc.wro.config.ReadOnlyContext; import ro.isdc.wro.model.group.Inject; import ro.isdc.wro.model.resource.processor.support.ImageUrlRewriter; import ro.isdc.wro.model.resource.processor.support.ImageUrlRewriter.RewriterContext; import ro.isdc.wro.model.resource.support.MutableResourceAuthorizationManager; import ro.isdc.wro.model.resource.support.ResourceAuthorizationManager; /** * Note: When used together with {@link CssImportPreProcessor}, the {@link CssUrlRewritingProcessor} should come first, * otherwise it will produce wrong results. *

* Rewrites background images url of the provided css content. This implementation takes care of most common cases such * as those described bellow: *

*

Conflicting content
 * 
../1.jpg
../1.jpg../../1.jpg
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Css resource URIImage URLComputed Image URL
ANY[URL]/1.jpg[URL]/1.jpg
/1.css/a/1.jpg/a/1.jpg
/1.jpg/1.jpg
1.jpg../1.jpg
../1.jpg../../1.jpg
/WEB-INF/1.css/a/1.jpg/a/1.jpg
/1.jpg/1.jpg
1.jpg[WRO-PREFIX]?id=/WEB-INF/1.jpg
../1.jpg[WRO-PREFIX]?id=/WEB-INF/../1.jpg
[X]/1.css
*
* where [X] is URL or a classpath resource
* where [WRO-PREFIX] is a servletContext prefix
* which will map WRO filter to the result url.
/a/1.jpg[WRO-PREFIX]?id=[X]/a/1.jpg
/1.jpg[WRO-PREFIX]?id=[X]/1.jpg
1.jpg[WRO-PREFIX]?id=[X]/1.jpg
../1.jpg[WRO-PREFIX]?id=[X]/../1.jpg
*

* [X]/1.css
* The algorithm requires two types of {@link UriLocator} objects, one for resolving url resources & one for classpath * resources. Both need to be injected using IoC when creating the instance of {@link CssUrlRewritingProcessor} class. * * @author Alex Objelean * @created Nov 19, 2008 */ public class CssUrlRewritingProcessor extends AbstractCssUrlRewritingProcessor { private static final Logger LOG = LoggerFactory.getLogger(CssUrlRewritingProcessor.class); public static final String ALIAS = "cssUrlRewriting"; @Inject private ResourceAuthorizationManager authorizationManager; @Inject private ReadOnlyContext context; /** * {@inheritDoc} */ @Override protected void onUrlReplaced(final String replacedUrl) { final String allowedUrl = StringUtils.removeStart(replacedUrl, getUrlPrefix()); LOG.debug("adding allowed url: {}", allowedUrl); //add only if add is supported if (authorizationManager instanceof MutableResourceAuthorizationManager) { ((MutableResourceAuthorizationManager) authorizationManager).add(allowedUrl); } } /** * Replace provided url with the new url if needed. * * @param cssUri * Uri of the parsed css. * @param imageUrl * to replace. * @return replaced url. */ @Override protected String replaceImageUrl(final String cssUri, final String imageUrl) { ======= /* * Copyright (c) 2008. All rights reserved. */ package ro.isdc.wro.model.resource.processor.impl.css; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ro.isdc.wro.config.ReadOnlyContext; import ro.isdc.wro.model.group.Inject; import ro.isdc.wro.model.resource.locator.UriLocator; import ro.isdc.wro.model.resource.processor.support.ImageUrlRewriter; import ro.isdc.wro.model.resource.processor.support.ImageUrlRewriter.RewriterContext; import ro.isdc.wro.model.resource.support.MutableResourceAuthorizationManager; import ro.isdc.wro.model.resource.support.ResourceAuthorizationManager; /** * Note: When used together with {@link CssImportPreProcessor}, the {@link CssUrlRewritingProcessor} should come first, * otherwise it will produce wrong results. *

* Rewrites background images url of the provided css content. This implementation takes care of most common cases such * as those described bellow: *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * where [X] is URL or a classpath resource
* where [WRO-PREFIX] is a servletContext prefix
* which will map WRO filter to the result url. * * * * * * * * * * * * * * * * *
Css resource URIImage URLComputed Image URL
ANY[URL]/1.jpg[URL]/1.jpg
/1.css/a/1.jpg/a/1.jpg
/1.jpg/1.jpg
1.jpg/WEB-INF/1.css/a/1.jpg/a/1.jpg
/1.jpg/1.jpg
1.jpg[WRO-PREFIX]?id=/WEB-INF/1.jpg
../1.jpg[WRO-PREFIX]?id=/WEB-INF/../1.jpg
/a/1.jpg[WRO-PREFIX]?id=[X]/a/1.jpg
/1.jpg[WRO-PREFIX]?id=[X]/1.jpg
1.jpg[WRO-PREFIX]?id=[X]/1.jpg
../1.jpg[WRO-PREFIX]?id=[X]/../1.jpg
*

* The algorithm requires two types of {@link UriLocator} objects, one for resolving url resources & one for classpath * resources. Both need to be injected using IoC when creating the instance of {@link CssUrlRewritingProcessor} class. * * @author Alex Objelean * @created Nov 19, 2008 */ public class CssUrlRewritingProcessor extends AbstractCssUrlRewritingProcessor { private static final Logger LOG = LoggerFactory.getLogger(CssUrlRewritingProcessor.class); public static final String ALIAS = "cssUrlRewriting"; @Inject private ResourceAuthorizationManager authorizationManager; @Inject private ReadOnlyContext context; @Override protected void onUrlReplaced(final String replacedUrl) { final String allowedUrl = StringUtils.removeStart(replacedUrl, getUrlPrefix()); LOG.debug("adding allowed url: {}", allowedUrl); //add only if add is supported if (authorizationManager instanceof MutableResourceAuthorizationManager) { ((MutableResourceAuthorizationManager) authorizationManager).add(allowedUrl); } } /** * Replace provided url with the new url if needed. * * @param cssUri * Uri of the parsed css. * @param imageUrl * to replace. * @return replaced url. */ @Override protected String replaceImageUrl(final String cssUri, final String imageUrl) { >>>>>>> 2f3dc582f9006a6d78f01d6a0cf78d8f1d5e0616 //Can be null when using standalone context. final String contextPath = context.getRequest() != null ? context.getRequest().getContextPath() : null; final RewriterContext rewriterContext = new RewriterContext().setAggregatedFolderPath( * * * * * * * /* * Copyright (c) 2008. All rights reserved. */ package ro.isdc.wro.model.resource.processor.impl.css; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ro.isdc.wro.config.ReadOnlyContext; import ro.isdc.wro.model.group.Inject; import ro.isdc.wro.model.resource.processor.support.ImageUrlRewriter; import ro.isdc.wro.model.resource.processor.support.ImageUrlRewriter.RewriterContext; import ro.isdc.wro.model.resource.support.MutableResourceAuthorizationManager; import ro.isdc.wro.model.resource.support.ResourceAuthorizationManager; /** * Note: When used together with {@link CssImportPreProcessor}, the {@link CssUrlRewritingProcessor} should come first, * otherwise it will produce wrong results. *

* Rewrites background images url of the provided css content. This implementation takes care of most common cases such * as those described bellow: *

*

Solution content
 * 
/1.jpg
/1.jpg
1.jpg
../1.jpg
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Css resource URIImage URLComputed Image URL
ANY[URL]/1.jpg[URL]/1.jpg
/1.css/a/1.jpg/a/1.jpg
/1.jpg/1.jpg
1.jpg../1.jpg
../1.jpg../../1.jpg
/WEB-INF/1.css/a/1.jpg/a/1.jpg[WRO-PREFIX]?id=/WEB-INF/1.jpg
[WRO-PREFIX]?id=/WEB-INF/../1.jpg
[X]/1.css
*
* where [X] is URL or a classpath resource
* where [WRO-PREFIX] is a servletContext prefix
* which will map WRO filter to the result url.
/a/1.jpg[WRO-PREFIX]?id=[X]/a/1.jpg
/1.jpg[WRO-PREFIX]?id=[X]/1.jpg
1.jpg[WRO-PREFIX]?id=[X]/1.jpg
../1.jpg[WRO-PREFIX]?id=[X]/../1.jpg
*

* The algorithm requires two types of {@link UriLocator} objects, one for resolving url resources & one for classpath * resources. Both need to be injected using IoC when creating the instance of {@link CssUrlRewritingProcessor} class. * * @author Alex Objelean * @created Nov 19, 2008 */ public class CssUrlRewritingProcessor extends AbstractCssUrlRewritingProcessor { private static final Logger LOG = LoggerFactory.getLogger(CssUrlRewritingProcessor.class); public static final String ALIAS = "cssUrlRewriting"; @Inject private ResourceAuthorizationManager authorizationManager; @Inject private ReadOnlyContext context; @Override protected void onUrlReplaced(final String replacedUrl) { final String allowedUrl = StringUtils.removeStart(replacedUrl, getUrlPrefix()); LOG.debug("adding allowed url: {}", allowedUrl); //add only if add is supported if (authorizationManager instanceof MutableResourceAuthorizationManager) { ((MutableResourceAuthorizationManager) authorizationManager).add(allowedUrl); } } /** * Replace provided url with the new url if needed. * * @param cssUri * Uri of the parsed css. * @param imageUrl * to replace. * @return replaced url. */ @Override protected String replaceImageUrl(final String cssUri, final String imageUrl) { //Can be null when using standalone context. final String contextPath = context.getRequest() != null ? context.getRequest().getContextPath() : null; final RewriterContext rewriterContext = new RewriterContext().setAggregatedFolderPath( context.getAggregatedFolderPath()).setProxyPrefix(getUrlPrefix()).setContextPath(contextPath); return new ImageUrlRewriter(rewriterContext).rewrite(cssUri, imageUrl); } /** * @param uri * to check if is allowed. * @return true if passed argument is contained in allowed list. * @VisibleFortesting */ public final boolean isUriAllowed(final String uri) { return authorizationManager.isAuthorized(uri); } /** * {@inheritDoc} */ @Override protected void onProcessCompleted() { } }

File
CssUrlRewritingProcessor.java
Developer's decision
Manual
Kind of conflict
Annotation
Attribute
Class signature
Comment
Import
Method declaration
Method invocation
Method signature
Package declaration
Chunk
Conflicting content
  @Mock
  private ResourceLocatorFactory mockLocatorFactory;
  @Mock
<<<<<<< HEAD
  private ResourceLocator mockLocator;
  
=======
  private UriLocator mockUriLocator;

>>>>>>> 2f3dc582f9006a6d78f01d6a0cf78d8f1d5e0616
  private OutputStream outputStream;

  private ServletOutputStream servletOutputStream;
Solution content
  @Mock
  private ResourceLocatorFactory mockLocatorFactory;
  @Mock
  private ResourceLocator mockLocator;
  private OutputStream outputStream;
  private ServletOutputStream servletOutputStream;
File
TestResourceProxyRequestHandler.java
Developer's decision
Version 1
Kind of conflict
Attribute
Chunk
Conflicting content
        new BaseWroManagerFactory().setLocatorFactory(mockLocatorFactory).setResourceAuthorizationManager(
        mockAuthorizationManager)).build();
    injector.inject(victim);
<<<<<<< HEAD
    
    when(mockLocatorFactory.getLocator(anyString())).thenReturn(mockLocator);
    when(mockLocatorFactory.locate(anyString())).thenAnswer(new Answer() {
=======

    when(mockUriLocatorFactory.getInstance(anyString())).thenReturn(mockUriLocator);
    when(mockUriLocatorFactory.locate(anyString())).then(new Answer() {
>>>>>>> 2f3dc582f9006a6d78f01d6a0cf78d8f1d5e0616
      public InputStream answer(final InvocationOnMock invocation)
          throws Throwable {
        return mockLocator.getInputStream();
Solution content
        new BaseWroManagerFactory().setLocatorFactory(mockLocatorFactory).setResourceAuthorizationManager(
        mockAuthorizationManager)).build();
    injector.inject(victim);
    when(mockLocatorFactory.getLocator(anyString())).thenReturn(mockLocator);
    when(mockLocatorFactory.locate(anyString())).thenAnswer(new Answer() {
      public InputStream answer(final InvocationOnMock invocation)
          throws Throwable {
        return mockLocator.getInputStream();
File
TestResourceProxyRequestHandler.java
Developer's decision
Version 1
Kind of conflict
Method invocation
Chunk
Conflicting content
        return mockLocator.getInputStream();
      }
    });
<<<<<<< HEAD
    when(mockLocator.getInputStream()).thenReturn(WroUtil.EMPTY_STREAM);
    
=======
    when(mockUriLocator.locate(anyString())).thenReturn(WroUtil.EMPTY_STREAM);
>>>>>>> 2f3dc582f9006a6d78f01d6a0cf78d8f1d5e0616
    packagePath = WroUtil.toPackageAsFolder(this.getClass());

    // Setup response writer
Solution content
        return mockLocator.getInputStream();
      }
    });
    when(mockLocator.getInputStream()).thenReturn(WroUtil.EMPTY_STREAM);
    packagePath = WroUtil.toPackageAsFolder(this.getClass());
    // Setup response writer
File
TestResourceProxyRequestHandler.java
Developer's decision
Version 1
Kind of conflict
Method invocation
Chunk
Conflicting content
    final String resourceUri = "classpath:" + packagePath + "/" + "test.css";
    when(mockAuthorizationManager.isAuthorized(resourceUri)).thenReturn(true);
    when(request.getParameter(ResourceProxyRequestHandler.PARAM_RESOURCE_ID)).thenReturn(resourceUri);
<<<<<<< HEAD
    when(mockLocator.getInputStream()).thenReturn(new ClasspathResourceLocator(resourceUri).getInputStream());
    
=======
    when(mockUriLocator.locate(anyString())).thenReturn(new ClasspathUriLocator().locate(resourceUri));

>>>>>>> 2f3dc582f9006a6d78f01d6a0cf78d8f1d5e0616
    victim.handle(request, response);

    final String body = outputStream.toString();
Solution content
    final String resourceUri = "classpath:" + packagePath + "/" + "test.css";
    when(mockAuthorizationManager.isAuthorized(resourceUri)).thenReturn(true);
    when(request.getParameter(ResourceProxyRequestHandler.PARAM_RESOURCE_ID)).thenReturn(resourceUri);
    when(mockLocator.getInputStream()).thenReturn(new ClasspathResourceLocator(resourceUri).getInputStream());
    victim.handle(request, response);
    final String body = outputStream.toString();
File
TestResourceProxyRequestHandler.java
Developer's decision
Version 1
Kind of conflict
Method invocation
Chunk
Conflicting content
    final String resourceUri = "classpath:" + packagePath + "/" + "test.css";
    when(mockAuthorizationManager.isAuthorized(resourceUri)).thenReturn(false);
    when(request.getParameter(ResourceProxyRequestHandler.PARAM_RESOURCE_ID)).thenReturn(resourceUri);
<<<<<<< HEAD
    when(mockLocator.getInputStream()).thenReturn(new ClasspathResourceLocator(resourceUri).getInputStream());
    
=======
    when(mockUriLocator.locate(anyString())).thenReturn(new ClasspathUriLocator().locate(resourceUri));

>>>>>>> 2f3dc582f9006a6d78f01d6a0cf78d8f1d5e0616
    victim.handle(request, response);
  }
Solution content
    final String resourceUri = "classpath:" + packagePath + "/" + "test.css";
    when(mockAuthorizationManager.isAuthorized(resourceUri)).thenReturn(false);
    when(request.getParameter(ResourceProxyRequestHandler.PARAM_RESOURCE_ID)).thenReturn(resourceUri);
    when(mockLocator.getInputStream()).thenReturn(new ClasspathResourceLocator(resourceUri).getInputStream());
    victim.handle(request, response);
  }
File
TestResourceProxyRequestHandler.java
Developer's decision
Version 1
Kind of conflict
Method invocation
Chunk
Conflicting content
    victim.handle(request, response);
    final String body = outputStream.toString();
    final String expectedBody = IOUtils.toString(getInputStream("test.css"));
<<<<<<< HEAD
    
    verify(mockLocator, times(1)).getInputStream();
=======

    verify(mockUriLocator, times(1)).locate(resourceUri);
>>>>>>> 2f3dc582f9006a6d78f01d6a0cf78d8f1d5e0616
    assertEquals(expectedBody, body);
  }
Solution content
    victim.handle(request, response);
    final String body = outputStream.toString();
    final String expectedBody = IOUtils.toString(getInputStream("test.css"));
    verify(mockLocator, times(1)).getInputStream();
    assertEquals(expectedBody, body);
  }
File
TestResourceProxyRequestHandler.java
Developer's decision
Version 1
Kind of conflict
Method invocation
Chunk
Conflicting content
    final String resourceUri = "classpath:" + packagePath + "/" + "test.css";
    when(mockAuthorizationManager.isAuthorized(resourceUri)).thenReturn(true);
    when(request.getParameter(ResourceProxyRequestHandler.PARAM_RESOURCE_ID)).thenReturn(resourceUri);
<<<<<<< HEAD
    when(mockLocator.getInputStream()).thenReturn(new ClasspathResourceLocator(resourceUri).getInputStream());
    
=======
    when(mockUriLocator.locate(anyString())).thenReturn(new ClasspathUriLocator().locate(resourceUri));

>>>>>>> 2f3dc582f9006a6d78f01d6a0cf78d8f1d5e0616
    victim.handle(request, response);
    final int expectedLength = IOUtils.toString(getInputStream("test.css")).length();
Solution content
    final String resourceUri = "classpath:" + packagePath + "/" + "test.css";
    when(mockAuthorizationManager.isAuthorized(resourceUri)).thenReturn(true);
    when(request.getParameter(ResourceProxyRequestHandler.PARAM_RESOURCE_ID)).thenReturn(resourceUri);
    when(mockLocator.getInputStream()).thenReturn(new ClasspathResourceLocator(resourceUri).getInputStream());
    victim.handle(request, response);
    final int expectedLength = IOUtils.toString(getInputStream("test.css")).length();
File
TestResourceProxyRequestHandler.java
Developer's decision
Version 1
Kind of conflict
Method invocation