Skip to content

Commit ecd31dd

Browse files
committed
Add redirect to avoid http 500
1 parent 48b8c9c commit ecd31dd

File tree

3 files changed

+95
-20
lines changed

3 files changed

+95
-20
lines changed

services/alarm-logger/src/main/java/org/phoebus/alarm/logging/rest/SearchController.java

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
import co.elastic.clients.elasticsearch.ElasticsearchClient;
44
import co.elastic.clients.elasticsearch._types.ElasticsearchVersionInfo;
55
import co.elastic.clients.elasticsearch.core.InfoResponse;
6-
76
import com.fasterxml.jackson.core.JsonProcessingException;
87
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import io.swagger.v3.oas.annotations.Operation;
9+
import io.swagger.v3.oas.annotations.Parameter;
10+
import io.swagger.v3.oas.annotations.Parameters;
11+
import io.swagger.v3.oas.annotations.media.Schema;
12+
import io.swagger.v3.oas.annotations.tags.Tag;
913
import org.phoebus.alarm.logging.AlarmLoggingService;
1014
import org.phoebus.alarm.logging.ElasticClientHelper;
1115
import org.springframework.beans.factory.annotation.Value;
@@ -18,6 +22,7 @@
1822
import org.springframework.web.bind.annotation.RestController;
1923
import org.springframework.web.server.ResponseStatusException;
2024

25+
import javax.servlet.http.HttpServletResponse;
2126
import java.io.IOException;
2227
import java.util.HashMap;
2328
import java.util.LinkedHashMap;
@@ -26,12 +31,6 @@
2631
import java.util.logging.Level;
2732
import java.util.logging.Logger;
2833

29-
import io.swagger.v3.oas.annotations.Operation;
30-
import io.swagger.v3.oas.annotations.tags.Tag;
31-
import io.swagger.v3.oas.annotations.media.Schema;
32-
import io.swagger.v3.oas.annotations.Parameter;
33-
import io.swagger.v3.oas.annotations.Parameters;
34-
3534
/**
3635
* A REST service for querying the alarm message history
3736
*
@@ -85,16 +84,16 @@ public String info() {
8584

8685
@Operation(summary = "Search alarms")
8786
@Parameters({
88-
@Parameter(name = "pv", description = "PV name", schema = @Schema(type = "string"), required = false, example = "*"),
89-
@Parameter(name = "severity", description = "Alarm severity", schema = @Schema(type = "string"), required = false, example = "*"),
90-
@Parameter(name = "message", description = "Alarm message", schema = @Schema(type = "string"), required = false, example = "*"),
91-
@Parameter(name = "current_severity", description = "PV severity", schema = @Schema(type = "string"), required = false, example = "*"),
92-
@Parameter(name = "current_message", description = "PV message", schema = @Schema(type = "string"), required = false, example = "*"),
93-
@Parameter(name = "user", description = "User", schema = @Schema(type = "string"), required = false, example = "*"),
94-
@Parameter(name = "host", description = "Host", schema = @Schema(type = "string"), required = false, example = "*"),
95-
@Parameter(name = "command", description = "Command", schema = @Schema(type = "string"), required = false, example = "*"),
96-
@Parameter(name = "start", description = "Start time", schema = @Schema(type = "string"), required = false, example = "2024-06-12"),
97-
@Parameter(name = "end", description = "End time", schema = @Schema(type = "string"), required = false, example = "2024-06-14"),
87+
@Parameter(name = "pv", description = "PV name", schema = @Schema(type = "string"), required = false, example = "*"),
88+
@Parameter(name = "severity", description = "Alarm severity", schema = @Schema(type = "string"), required = false, example = "*"),
89+
@Parameter(name = "message", description = "Alarm message", schema = @Schema(type = "string"), required = false, example = "*"),
90+
@Parameter(name = "current_severity", description = "PV severity", schema = @Schema(type = "string"), required = false, example = "*"),
91+
@Parameter(name = "current_message", description = "PV message", schema = @Schema(type = "string"), required = false, example = "*"),
92+
@Parameter(name = "user", description = "User", schema = @Schema(type = "string"), required = false, example = "*"),
93+
@Parameter(name = "host", description = "Host", schema = @Schema(type = "string"), required = false, example = "*"),
94+
@Parameter(name = "command", description = "Command", schema = @Schema(type = "string"), required = false, example = "*"),
95+
@Parameter(name = "start", description = "Start time", schema = @Schema(type = "string"), required = false, example = "2024-06-12"),
96+
@Parameter(name = "end", description = "End time", schema = @Schema(type = "string"), required = false, example = "2024-06-14"),
9897
})
9998
@RequestMapping(value = "/search/alarm", method = RequestMethod.GET)
10099
public List<AlarmLogMessage> search(@Parameter(hidden = true) @RequestParam Map<String, String> allRequestParams) {
@@ -114,18 +113,29 @@ public List<AlarmLogMessage> searchPv(@Parameter(description = "PV name") @PathV
114113
@Operation(summary = "Search alarm config")
115114
@Schema(name = "config", example = "/Accelerator/compteur", required = true)
116115
@Parameters({
117-
@Parameter(name = "config", description = "Config path", schema = @Schema(type = "string"), required = false, example = "/Accelerator/pvname"),
116+
@Parameter(name = "config", description = "Config path", schema = @Schema(type = "string"), required = false, example = "/Accelerator/pvname"),
118117
})
119118
@RequestMapping(value = "/search/alarm/config", method = RequestMethod.GET)
120119
public List<AlarmLogMessage> searchConfig(@Parameter(hidden = true) @RequestParam Map<String, String> allRequestParams) {
121-
if(allRequestParams == null ||
120+
if (allRequestParams == null ||
122121
allRequestParams.isEmpty() ||
123122
!allRequestParams.containsKey("config") ||
124-
allRequestParams.get("config").isEmpty()){
123+
allRequestParams.get("config").isEmpty()) {
125124
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
126125
}
127126
List<AlarmLogMessage> result = AlarmLogSearchUtil.searchConfig(ElasticClientHelper.getInstance().getClient(), allRequestParams);
128127
return result;
129128
}
130129

130+
/**
131+
* Handles the /swagger-ui URL: redirects to /swagger-ui/index.html to avoid 500 response.
132+
*
133+
* @param response The {@link HttpServletResponse} to configure with a redirect (301).
134+
*/
135+
@GetMapping("/swagger-ui")
136+
public void api(HttpServletResponse response) {
137+
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
138+
response.addHeader("Location", "/swagger-ui/index.html");
139+
}
140+
131141
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright (C) 2018 European Spallation Source ERIC.
3+
* <p>
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation; either version 2
7+
* of the License, or (at your option) any later version.
8+
* <p>
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
* <p>
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+
*/
18+
19+
package org.phoebus.alarm.logging.rest;
20+
21+
import org.springframework.boot.SpringBootConfiguration;
22+
import org.springframework.context.annotation.ComponentScan;
23+
24+
@SpringBootConfiguration
25+
@SuppressWarnings("unused")
26+
@ComponentScan(basePackages = "org.phoebus.alarm.logging.rest")
27+
public class ControllersTestConfig {
28+
29+
30+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (C) 2025 European Spallation Source ERIC.
3+
*/
4+
5+
package org.phoebus.alarm.logging.rest;
6+
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.api.extension.ExtendWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
11+
import org.springframework.test.context.ContextConfiguration;
12+
import org.springframework.test.context.junit.jupiter.SpringExtension;
13+
import org.springframework.test.web.servlet.MockMvc;
14+
import org.springframework.test.web.servlet.ResultActions;
15+
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
16+
17+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
18+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
19+
import static org.junit.jupiter.api.Assertions.*;
20+
21+
@ExtendWith(SpringExtension.class)
22+
@ContextConfiguration(classes = ControllersTestConfig.class)
23+
@WebMvcTest(SearchController.class)
24+
public class SearchControllerTest {
25+
26+
@Autowired
27+
private MockMvc mockMvc;
28+
29+
@Test
30+
public void testRedirectSwagger() throws Exception {
31+
MockHttpServletRequestBuilder request = get("/swagger-ui");
32+
ResultActions resultActions = mockMvc.perform(request).andExpect(status().isMovedPermanently());
33+
assertEquals("/swagger-ui/index.html", resultActions.andReturn().getResponse().getHeader("Location"));
34+
}
35+
}

0 commit comments

Comments
 (0)