org.springframework.util.unit.DataSize Java Examples

The following examples show how to use org.springframework.util.unit.DataSize. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MultipartConfig.java    From springboot-link-admin with Apache License 2.0 7 votes vote down vote up
@Bean
public MultipartConfigElement multipartConfigElement() {
	MultipartConfigFactory factory = new MultipartConfigFactory();
	// 单个文件最大
	factory.setMaxFileSize(DataSize.of(100, DataUnit.MEGABYTES)); // 100MB
	// / 设置总上传数据总大小
	factory.setMaxRequestSize(DataSize.of(100, DataUnit.MEGABYTES));// 100MB
	return factory.createMultipartConfig();
}
 
Example #2
Source File: Common.java    From nimrod with MIT License 6 votes vote down vote up
public void initialize() {
        // 首次启动加载数据字典到 ServletContext 内存
        dictionaryService.addDictionaryToServletContext();
        mailService.initialize();
        // 将待发送的邮件重新加入到发送队列
        mailService.retry(false);

        String maxFileSize = (String) dictionaryService.get("FILE", "MAX_FILE_SIZE");
        String maxRequestSize = (String) dictionaryService.get("FILE", "MAX_REQUEST_SIZE");
        updatableMultipartConfigElement.setMaxFileSize(DataSize.parse(maxFileSize).toBytes());
        updatableMultipartConfigElement.setMaxRequestSize(DataSize.parse(maxRequestSize).toBytes());

        String timeZoneId = (String) dictionaryService.get("SYSTEM", "TIME_ZONE_ID");
        TimeZone.setDefault(TimeZone.getTimeZone(timeZoneId));
//        Calendar.getInstance(TimeZone.getTimeZone());
//        DateFormat.getDateTimeInstance().setTimeZone(TimeZone.getTimeZone(timeZoneId));
    }
 
Example #3
Source File: ArmeriaReactiveWebServerFactoryTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
void shouldReturnCompressedResponse() {
    final ArmeriaReactiveWebServerFactory factory = factory();
    final Compression compression = new Compression();
    compression.setEnabled(true);
    compression.setMinResponseSize(DataSize.ofBytes(1));
    compression.setMimeTypes(new String[] { "text/plain" });
    compression.setExcludedUserAgents(new String[] { "unknown-agent/[0-9]+\\.[0-9]+\\.[0-9]+$" });
    factory.setCompression(compression);
    runEchoServer(factory, server -> {
        final AggregatedHttpResponse res = sendPostRequest(httpClient(server));
        assertThat(res.status()).isEqualTo(com.linecorp.armeria.common.HttpStatus.OK);
        assertThat(res.headers().get(HttpHeaderNames.CONTENT_ENCODING)).isEqualTo("gzip");
        assertThat(res.contentUtf8()).isNotEqualTo("hello");
    });
}
 
Example #4
Source File: AbstractChannelFactory.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
/**
 * Configures limits such as max message sizes that should be used by the channel.
 *
 * @param builder The channel builder to configure.
 * @param name The name of the client to configure.
 */
protected void configureLimits(final T builder, final String name) {
    final GrpcChannelProperties properties = getPropertiesFor(name);
    final DataSize maxInboundMessageSize = properties.getMaxInboundMessageSize();
    if (maxInboundMessageSize != null) {
        builder.maxInboundMessageSize((int) maxInboundMessageSize.toBytes());
    }
}
 
Example #5
Source File: ArmeriaReactiveWebServerFactoryTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void shouldReturnNonCompressedResponse_dueToUserAgent() {
    final ArmeriaReactiveWebServerFactory factory = factory();
    final Compression compression = new Compression();
    compression.setEnabled(true);
    compression.setMinResponseSize(DataSize.ofBytes(1));
    compression.setExcludedUserAgents(new String[] { "test-agent/[0-9]+\\.[0-9]+\\.[0-9]+$" });
    factory.setCompression(compression);
    runEchoServer(factory, server -> {
        final AggregatedHttpResponse res = sendPostRequest(httpClient(server));
        validateEchoResponse(res);
        assertThat(res.headers().get(HttpHeaderNames.CONTENT_ENCODING)).isNull();
    });
}
 
Example #6
Source File: AbstractChannelFactory.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
/**
 * Configures limits such as max message sizes that should be used by the channel.
 *
 * @param builder The channel builder to configure.
 * @param name The name of the client to configure.
 */
protected void configureLimits(final T builder, final String name) {
    final GrpcChannelProperties properties = getPropertiesFor(name);
    final DataSize maxInboundMessageSize = properties.getMaxInboundMessageSize();
    if (maxInboundMessageSize != null) {
        builder.maxInboundMessageSize((int) maxInboundMessageSize.toBytes());
    }
}
 
Example #7
Source File: AbstractGrpcServerFactory.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
/**
 * Configures limits such as max message sizes that should be used by the server.
 *
 * @param builder The server builder to configure.
 */
protected void configureLimits(final T builder) {
    final DataSize maxInboundMessageSize = this.properties.getMaxInboundMessageSize();
    if (maxInboundMessageSize != null) {
        builder.maxInboundMessageSize((int) maxInboundMessageSize.toBytes());
    }
}
 
Example #8
Source File: ArmeriaReactiveWebServerFactoryTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void shouldReturnNonCompressedResponse_dueToContentType() {
    final ArmeriaReactiveWebServerFactory factory = factory();
    final Compression compression = new Compression();
    compression.setEnabled(true);
    compression.setMinResponseSize(DataSize.ofBytes(1));
    compression.setMimeTypes(new String[] { "text/html" });
    factory.setCompression(compression);
    runEchoServer(factory, server -> {
        final AggregatedHttpResponse res = sendPostRequest(httpClient(server));
        validateEchoResponse(res);
        assertThat(res.headers().get(HttpHeaderNames.CONTENT_ENCODING)).isNull();
    });
}
 
Example #9
Source File: MailModuleTestWithAllProperties.java    From code-examples with MIT License 5 votes vote down vote up
@Test
void propertiesAreLoaded() {
    assertThat(mailModuleProperties).isNotNull();
    assertThat(mailModuleProperties.getDefaultSubject()).isEqualTo("hello");
    assertThat(mailModuleProperties.getEnabled()).isTrue();
    assertThat(mailModuleProperties.getPauseBetweenMails()).isEqualByComparingTo(Duration.ofSeconds(5));
    assertThat(mailModuleProperties.getMaxAttachmentSize()).isEqualByComparingTo(DataSize.ofMegabytes(1));
    assertThat(mailModuleProperties.getSmtpServers()).hasSize(2);
    assertThat(mailModuleProperties.getMaxAttachmentWeight().getGrams()).isEqualTo(5000L);
}
 
Example #10
Source File: RequestHeaderSizeGatewayFilterFactoryTest.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Bean
public RouteLocator testRouteLocator(RouteLocatorBuilder builder) {
	return builder.routes().route("test_request_header_size",
			r -> r.order(-1).host("**.test.org").filters(
					f -> f.setRequestHeaderSize(DataSize.of(46L, DataUnit.BYTES)))
					.uri(uri))
			.build();
}
 
Example #11
Source File: MaxDataSizeValidator.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isValid(DataSize value, ConstraintValidatorContext context) {
	// null values are valid
	if (value == null) {
		return true;
	}
	return value.toBytes() <= maxValue;
}
 
Example #12
Source File: RequestHeaderSizeGatewayFilterFactoryTest.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Test
public void toStringFormat() {
	Config config = new Config();
	config.setMaxSize(DataSize.ofBytes(1000L));
	GatewayFilter filter = new RequestHeaderSizeGatewayFilterFactory().apply(config);
	assertThat(filter.toString()).contains("max").contains("1000B");
}
 
Example #13
Source File: RequestSizeGatewayFilterFactoryTest.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Test
public void toStringFormat() {
	RequestSizeConfig config = new RequestSizeConfig();
	config.setMaxSize(DataSize.ofBytes(1000L));
	GatewayFilter filter = new RequestSizeGatewayFilterFactory().apply(config);
	assertThat(filter.toString()).contains("max").contains("1000");
}
 
Example #14
Source File: AbstractGrpcServerFactory.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
/**
 * Configures limits such as max message sizes that should be used by the server.
 *
 * @param builder The server builder to configure.
 */
protected void configureLimits(final T builder) {
    final DataSize maxInboundMessageSize = this.properties.getMaxInboundMessageSize();
    if (maxInboundMessageSize != null) {
        builder.maxInboundMessageSize((int) maxInboundMessageSize.toBytes());
    }
}
 
Example #15
Source File: SystemConfig.java    From redis-manager with Apache License 2.0 5 votes vote down vote up
@Bean
public MultipartConfigElement multipartConfigElement() {
    MultipartConfigFactory factory = new MultipartConfigFactory();
    //文件最大KB,MB
    factory.setMaxFileSize(DataSize.ofBytes(10485760));
    //设置总上传数据总大小
    factory.setMaxRequestSize(DataSize.ofBytes(10485760));
    return factory.createMultipartConfig();
}
 
Example #16
Source File: RestControllerAdviceHandler.java    From nimrod with MIT License 5 votes vote down vote up
@ExceptionHandler(MultipartException.class)
public ResponseEntity<FailureEntity> sizeLimitExceededExceptionHandler(HttpServletRequest httpServletRequest, Throwable throwable) {
    HttpStatus httpStatus = getStatus(httpServletRequest);
    FailureEntity fm = failureEntity.i18n("file.upload_fail");
    if (throwable instanceof MaxUploadSizeExceededException) {
        String maxFileSize = DataSizeUtil.pretty(DataSize.parse((String) dictionaryService.get("FILE", "MAX_FILE_SIZE")).toBytes());
        String maxRequestSize = DataSizeUtil.pretty(DataSize.parse((String) dictionaryService.get("FILE", "MAX_REQUEST_SIZE")).toBytes());
        fm = failureEntity.i18n("file.upload_fail_max_upload_size_exceeded", maxFileSize, maxRequestSize);
    }
    throwable.printStackTrace();
    return new ResponseEntity<>(fm, httpStatus);
}
 
Example #17
Source File: RequestHeaderSizeGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public void setMaxSize(DataSize maxSize) {
	this.maxSize = maxSize;
}
 
Example #18
Source File: RequestSizeGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public RequestSizeGatewayFilterFactory.RequestSizeConfig setMaxSize(
		DataSize maxSize) {
	this.maxSize = maxSize;
	return this;
}
 
Example #19
Source File: RequestSizeGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public DataSize getMaxSize() {
	return maxSize;
}
 
Example #20
Source File: JobMonitorServiceImpl.java    From genie with Apache License 2.0 4 votes vote down vote up
private void checkFilesSize(final Path jobDirectory) {
    final DirectoryManifest manifest;
    try {
        manifest = this.manifestCreatorService.getDirectoryManifest(jobDirectory);
    } catch (IOException e) {
        log.warn("Failed to obtain manifest: {}" + e.getMessage());
        return;
    }

    final int files = manifest.getNumFiles();
    final int maxFiles = this.properties.getMaxFiles();
    if (files > maxFiles) {
        log.error("Limit exceeded, too many files: {}/{}", files, maxFiles);
        this.killService.kill(KillService.KillSource.FILES_LIMIT);
        return;
    }

    final DataSize totalSize = DataSize.ofBytes(manifest.getTotalSizeOfFiles());
    final DataSize maxTotalSize = this.properties.getMaxTotalSize();
    if (totalSize.toBytes() > maxTotalSize.toBytes()) {
        log.error("Limit exceeded, job directory too large: {}/{}", totalSize, maxTotalSize);
        this.killService.kill(KillService.KillSource.FILES_LIMIT);
        return;
    }

    final Optional<DirectoryManifest.ManifestEntry> largestFile = manifest.getFiles()
        .stream()
        .max(Comparator.comparing(DirectoryManifest.ManifestEntry::getSize));

    if (largestFile.isPresent()) {
        final DataSize largestFileSize = DataSize.ofBytes(largestFile.get().getSize());
        final DataSize maxFileSize = this.properties.getMaxFileSize();
        if (largestFileSize.toBytes() > maxFileSize.toBytes()) {
            log.error(
                "Limit exceeded, file too large: {}/{} ({})",
                largestFileSize,
                maxFileSize,
                largestFile.get().getPath()
            );
            this.killService.kill(KillService.KillSource.FILES_LIMIT);
            return;
        }
    }

    log.debug("No files limit exceeded");
}
 
Example #21
Source File: PropertyConversion.java    From tutorials with MIT License 4 votes vote down vote up
public DataSize getSizeInDefaultUnit() {
    return sizeInDefaultUnit;
}
 
Example #22
Source File: PropertyConversion.java    From tutorials with MIT License 4 votes vote down vote up
public void setSizeInDefaultUnit(DataSize sizeInDefaultUnit) {
    this.sizeInDefaultUnit = sizeInDefaultUnit;
}
 
Example #23
Source File: PropertyConversion.java    From tutorials with MIT License 4 votes vote down vote up
public DataSize getSizeInGB() {
    return sizeInGB;
}
 
Example #24
Source File: PropertyConversion.java    From tutorials with MIT License 4 votes vote down vote up
public void setSizeInGB(DataSize sizeInGB) {
    this.sizeInGB = sizeInGB;
}
 
Example #25
Source File: PropertyConversion.java    From tutorials with MIT License 4 votes vote down vote up
public DataSize getSizeInTB() {
    return sizeInTB;
}
 
Example #26
Source File: PropertyConversion.java    From tutorials with MIT License 4 votes vote down vote up
public void setSizeInTB(DataSize sizeInTB) {
    this.sizeInTB = sizeInTB;
}
 
Example #27
Source File: PropertiesConversionIntegrationTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenUseDataSizePropertyConversion_thenSuccess() throws Exception {
    assertEquals(DataSize.ofBytes(300), properties.getSizeInDefaultUnit());
    assertEquals(DataSize.ofGigabytes(2), properties.getSizeInGB());
    assertEquals(DataSize.ofTerabytes(4), properties.getSizeInTB());        
}
 
Example #28
Source File: PropertyConversion.java    From tutorials with MIT License 4 votes vote down vote up
public DataSize getUploadSpeed() {
    return uploadSpeed;
}
 
Example #29
Source File: PropertyConversion.java    From tutorials with MIT License 4 votes vote down vote up
public void setUploadSpeed(DataSize uploadSpeed) {
    this.uploadSpeed = uploadSpeed;
}
 
Example #30
Source File: PropertyConversion.java    From tutorials with MIT License 4 votes vote down vote up
public DataSize getDownloadSpeed() {
    return downloadSpeed;
}