Search in sources :

Example 1 with DeletableResource

use of org.talend.daikon.content.DeletableResource in project data-prep by Talend.

the class CloseableInputStreamComponent method getDeletableResource.

public DeletableResource getDeletableResource() throws IOException {
    final DeletableResource deletableResource = Mockito.mock(DeletableResource.class);
    when(deletableResource.getInputStream()).thenReturn(getInput());
    when(deletableResource.getOutputStream()).thenReturn(getOutput());
    return deletableResource;
}
Also used : DeletableResource(org.talend.daikon.content.DeletableResource)

Example 2 with DeletableResource

use of org.talend.daikon.content.DeletableResource in project data-prep by Talend.

the class CloseableResourceWatch method closeableWatch.

@Around("within(org.talend..*) && (execution(public java.io.Closeable+ *(..)) || execution(public org.talend.daikon.content.DeletableResource+ *(..)))")
public Object closeableWatch(ProceedingJoinPoint pjp) throws Throwable {
    final Object proceed = pjp.proceed();
    if (proceed == null) {
        LOGGER.warn("Unable to watch null closeable.");
        return null;
    }
    try {
        if (proceed instanceof InputStream) {
            final CloseableHandler handler = new InputStreamHandler((InputStream) proceed);
            addEntry(handler);
            return handler;
        } else if (proceed instanceof OutputStream) {
            final CloseableHandler handler = new OutputStreamHandler((OutputStream) proceed);
            addEntry(handler);
            return handler;
        } else if (proceed instanceof DeletableResource) {
            ProxyFactory proxyFactory = new ProxyFactory(proceed);
            proxyFactory.addAdvice(new ClosableMethodInterceptor());
            return proxyFactory.getProxy();
        } else {
            LOGGER.warn("No watch for '{}'.", proceed);
            return proceed;
        }
    } catch (Exception e) {
        if (!LOGGER.isDebugEnabled()) {
            LOGGER.error("Unable to watch resource '{}'.", proceed);
        } else {
            LOGGER.debug("Unable to watch resource '{}'.", proceed, e);
        }
    }
    return proceed;
}
Also used : ProxyFactory(org.springframework.aop.framework.ProxyFactory) DeletableResource(org.talend.daikon.content.DeletableResource) Around(org.aspectj.lang.annotation.Around)

Example 3 with DeletableResource

use of org.talend.daikon.content.DeletableResource in project data-prep by Talend.

the class ResourceLoaderContentCache method evictMatch.

@Timed
@Override
public void evictMatch(ContentCacheKey key) {
    try {
        final DeletableResource[] resources = resolver.getResources("/cache/" + key.getPrefix() + "**");
        final Predicate<String> matcher = key.getMatcher();
        stream(resources).filter(r -> matcher.test(r.getFilename())).forEach(r -> {
            try {
                r.delete();
            } catch (IOException e) {
                throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e);
            }
        });
    } catch (IOException e) {
        throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e);
    }
}
Also used : VolumeMetered(org.talend.dataprep.metrics.VolumeMetered) StringUtils(org.apache.commons.lang.StringUtils) TDPException(org.talend.dataprep.exception.TDPException) ContentCacheKey(org.talend.dataprep.cache.ContentCacheKey) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) BinaryOperator.maxBy(java.util.function.BinaryOperator.maxBy) OutputStream(java.io.OutputStream) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) StringUtils.substringAfterLast(org.apache.commons.lang.StringUtils.substringAfterLast) Logger(org.slf4j.Logger) DeletableResource(org.talend.daikon.content.DeletableResource) Optional.ofNullable(java.util.Optional.ofNullable) Predicate(java.util.function.Predicate) IOException(java.io.IOException) ContentCache(org.talend.dataprep.cache.ContentCache) Component(org.springframework.stereotype.Component) ContentServiceEnabled(org.talend.daikon.content.ContentServiceEnabled) Optional(java.util.Optional) CommonErrorCodes(org.talend.dataprep.exception.error.CommonErrorCodes) ResourceResolver(org.talend.daikon.content.ResourceResolver) Long.parseLong(java.lang.Long.parseLong) Arrays.stream(java.util.Arrays.stream) Timed(org.talend.dataprep.metrics.Timed) InputStream(java.io.InputStream) TDPException(org.talend.dataprep.exception.TDPException) DeletableResource(org.talend.daikon.content.DeletableResource) IOException(java.io.IOException) Timed(org.talend.dataprep.metrics.Timed)

Example 4 with DeletableResource

use of org.talend.daikon.content.DeletableResource in project data-prep by Talend.

the class DataSetServiceTest method updateRawContentWithDirectoryTraversalFile_TDP_3505.

@Test
public void updateRawContentWithDirectoryTraversalFile_TDP_3505() throws Exception {
    // dataSetId is the filename under which the file will be stored on the server
    String dataSetIdFlawed = "foobar..\\..\\..\\a";
    // create dataset
    given().body(IOUtils.toString(this.getClass().getResourceAsStream(TAGADA_CSV), UTF_8)).queryParam("Content-Type", "text/csv").when().post("/datasets").asString();
    String body = IOUtils.toString(this.getClass().getResourceAsStream(TAGADA_CSV), UTF_8);
    // 
    given().body(body).when().queryParam("name", // 
    "toto").put("/datasets/{id}/raw", // 
    dataSetIdFlawed).then().statusCode(OK.value());
    // expectations
    DeletableResource resource = resolver.getResource("/store/datasets/content/dataset/" + dataSetIdFlawed);
    assertFalse(resource.getFile().exists());
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) DeletableResource(org.talend.daikon.content.DeletableResource) DataSetBaseTest(org.talend.dataprep.dataset.DataSetBaseTest) Test(org.junit.Test)

Aggregations

DeletableResource (org.talend.daikon.content.DeletableResource)4 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Long.parseLong (java.lang.Long.parseLong)1 Arrays.stream (java.util.Arrays.stream)1 Optional (java.util.Optional)1 Optional.ofNullable (java.util.Optional.ofNullable)1 BinaryOperator.maxBy (java.util.function.BinaryOperator.maxBy)1 Predicate (java.util.function.Predicate)1 StringUtils (org.apache.commons.lang.StringUtils)1 StringUtils.substringAfterLast (org.apache.commons.lang.StringUtils.substringAfterLast)1 Around (org.aspectj.lang.annotation.Around)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)1 Test (org.junit.Test)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1