Skip to content

Commit

Permalink
Merge pull request #169 from Olog/CSSTUDIO-1818
Browse files Browse the repository at this point in the history
Csstudio 1818
  • Loading branch information
georgweiss authored Jul 3, 2023
2 parents 585a730 + 93c6862 commit f0fcfed
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 63 deletions.
7 changes: 6 additions & 1 deletion src/main/java/org/phoebus/olog/Application.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package org.phoebus.olog;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.AbstractHttp11Protocol;
import org.phoebus.olog.notification.LogEntryNotifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
Expand Down Expand Up @@ -135,5 +141,4 @@ public AcceptHeaderResolver acceptHeaderResolver(){
public LogEntryValidator logEntryValidator(){
return new LogEntryValidator();
}

}
61 changes: 61 additions & 0 deletions src/main/java/org/phoebus/olog/FileUploadSizeExceededHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2020 European Spallation Source ERIC.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

package org.phoebus.olog;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;

/**
* Handles request exceeding configured sizes (spring.servlet.multipart.max-file-size and
* spring.servlet.multipart.max-request-size). In such cases client will get an HTTP 413 (payload too large) response
* with a (hopefully) useful message.
*/
@ControllerAdvice
@SuppressWarnings("unused")
public class FileUploadSizeExceededHandler {

/**
* Specifies the allowed origins for CORS requests. Defaults to http://localhost:3000,
* which is useful during development of the web front-end in NodeJS.
*/
@Value("${cors.allowed.origins:http://localhost:3000}")
private String corsAllowedOrigins;


@ExceptionHandler(MaxUploadSizeExceededException.class)
public ResponseEntity<String> handleMaxSizeExceededException(RuntimeException ex, WebRequest request) {
// These HTTP headers are needed by browsers in order to handle the 413 response properly.
HttpHeaders headers = new HttpHeaders();
headers.add("Access-Control-Allow-Origin", corsAllowedOrigins);
headers.add("Access-Control-Allow-Credentials", "true");
return new ResponseEntity<>("Log entry exceeds size limits",
headers,
HttpStatus.PAYLOAD_TOO_LARGE);
}
}
6 changes: 6 additions & 0 deletions src/main/java/org/phoebus/olog/HttpConnectorConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.phoebus.olog;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.AbstractHttp11Protocol;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
Expand All @@ -11,6 +12,7 @@

@Configuration
@PropertySource("classpath:/application.properties")
@SuppressWarnings("unused")
public class HttpConnectorConfig {

@Value("${server.http.enable:true}")
Expand All @@ -30,6 +32,10 @@ private Connector getHttpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(port);
// This is needed to be able to send a response if client uploads a log entry
// exceeding configured max sizes. Without this setting Tomcat will simply close the
// connection before a response can be sent.
((AbstractHttp11Protocol <?>)connector.getProtocolHandler()).setMaxSwallowSize(-1);
return connector;
}
}
20 changes: 17 additions & 3 deletions src/main/java/org/phoebus/olog/InfoResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.mongodb.client.MongoClient;
import org.apache.catalina.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.unit.DataSize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -23,6 +25,7 @@

@RestController
@RequestMapping(OLOG_SERVICE_INFO)
@SuppressWarnings("unused")
public class InfoResource
{

Expand All @@ -39,6 +42,12 @@ public class InfoResource
@Value("${elasticsearch.http.port:9200}")
private int port;

@Value("${spring.servlet.multipart.max-file-size:15MB}")
private String maxFileSize;

@Value("${spring.servlet.multipart.max-request-size:50MB}")
private String maxRequestSize;

private final static ObjectMapper objectMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);

/**
Expand All @@ -48,12 +57,12 @@ public class InfoResource
@GetMapping
public String info() {

Map<String, Object> ologServiceInfo = new LinkedHashMap<String, Object>();
Map<String, Object> ologServiceInfo = new LinkedHashMap<>();
ologServiceInfo.put("name", "Olog Service");
ologServiceInfo.put("version", version);

ElasticsearchClient client = esService.getClient();
Map<String, String> elasticInfo = new LinkedHashMap<String, String>();
Map<String, String> elasticInfo = new LinkedHashMap<>();
try {
InfoResponse response = client.info();
elasticInfo.put("status", "Connected");
Expand All @@ -70,6 +79,12 @@ public String info() {
ologServiceInfo.put("elastic", elasticInfo);
ologServiceInfo.put("mongoDB", mongoClient.getClusterDescription().getShortDescription());

Map<String, Object> serverConfigInfo = new LinkedHashMap<>();
// Provide sizes in MB, arithmetics needed to avoid rounding to 0.
serverConfigInfo.put("maxFileSize", 1.0 * DataSize.parse(maxFileSize).toKilobytes() / 1024);
serverConfigInfo.put("maxRequestSize", 1.0 * DataSize.parse(maxRequestSize).toKilobytes() / 1024);

ologServiceInfo.put("serverConfig", serverConfigInfo);

try {
return objectMapper.writeValueAsString(ologServiceInfo);
Expand All @@ -78,5 +93,4 @@ public String info() {
return "Failed to gather Olog service info";
}
}

}
Loading

0 comments on commit f0fcfed

Please sign in to comment.