Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan-WorkGH committed Jun 3, 2024
1 parent b19a4ac commit aa038ff
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ public ResponseEntity<Void> setShortCircuitParameters(
@ApiResponse(responseCode = "200", description = "The short-circuit analysis parameters", content = {
@Content(mediaType = MediaType.APPLICATION_JSON_VALUE /*, schema = ...*/)})
public ResponseEntity<String> getShortCircuitParameters(@PathVariable("studyUuid") UUID studyUuid) {
return ResponseEntity.ok().body(studyService.getShortCircuitParametersInfo(studyUuid));
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(studyService.getShortCircuitParametersInfo(studyUuid));
}

@GetMapping(value = "/studies/{studyUuid}/nodes/{nodeUuid}/network/substations/{substationId}/svg")
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/org/gridsuite/study/server/StudyWebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
package org.gridsuite.study.server;

import lombok.AllArgsConstructor;
import org.gridsuite.study.server.converter.UuidHttpConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@AllArgsConstructor
@Configuration
public class StudyWebConfig implements WebMvcConfigurer {
private InsensitiveStringToEnumConverterFactory insensitiveEnumConverterFactory;
private final InsensitiveStringToEnumConverterFactory insensitiveEnumConverterFactory;
private final UuidHttpConverter uuidConverter;

/**
* {@inheritDoc}
Expand All @@ -23,4 +28,12 @@ public class StudyWebConfig implements WebMvcConfigurer {
public void addFormatters(final FormatterRegistry registry) {
registry.addConverterFactory(insensitiveEnumConverterFactory);
}

/**
* {@inheritDoc}
*/
@Override
public void extendMessageConverters(final List<HttpMessageConverter<?>> converters) {
converters.add(uuidConverter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.gridsuite.study.server.converter;

import lombok.NonNull;
import org.jetbrains.annotations.NotNull;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.web.servlet.server.Encoding;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.MimeType;
import org.springframework.util.StreamUtils;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

/**
* Implementation of {@link HttpMessageConverter} that can read and write {@link UUID} to/from strings.
*
* @since 2.0
*/
@Component
public class UuidHttpConverter extends AbstractHttpMessageConverter<UUID> {
private final Encoding encoding;
private final List<Charset> availableCharsets = List.copyOf(Charset.availableCharsets().values());

protected UuidHttpConverter(@NonNull final Environment environment) {
super(StandardCharsets.UTF_8, MediaType.TEXT_PLAIN);
encoding = Binder.get(environment).bindOrCreate("server.servlet.encoding", Encoding.class);
}

/**
* {@inheritDoc}
*/
@Override
@NotNull
public Charset getDefaultCharset() {
return Objects.requireNonNullElse(encoding.getCharset(), super.getDefaultCharset());
}

/**
* {@inheritDoc}
*/
@Override
protected boolean supports(final Class<?> clazz) {
return UUID.class == clazz;
}

/**
* {@inheritDoc}
*/
@Override
@NotNull
protected Long getContentLength(UUID uuid, MediaType contentType) throws IOException {
return (long) uuid.toString().getBytes(getContentTypeCharset(contentType)).length;

}

/**
* {@inheritDoc}
*/
@Override
@NotNull
protected UUID readInternal(final Class<? extends UUID> clazz, final HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
return UUID.fromString(StreamUtils.copyToString(inputMessage.getBody(), getContentTypeCharset(inputMessage.getHeaders().getContentType())));
}

/**
* {@inheritDoc}
*/
@Override
protected void writeInternal(final UUID uuid, final HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
final HttpHeaders headers = outputMessage.getHeaders();
if (headers.get(HttpHeaders.ACCEPT_CHARSET) == null) {
headers.setAcceptCharset(this.availableCharsets);
}
StreamUtils.copy(uuid.toString(), this.getContentTypeCharset(headers.getContentType()), outputMessage.getBody());
}

@NotNull
private Charset getContentTypeCharset(@Nullable final MediaType contentType) {
return Objects.requireNonNull(Optional.ofNullable(contentType)
.map(MimeType::getCharset)
.orElseGet(this::getDefaultCharset), "No default charset");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,13 @@ public UUID createParameters(@Nullable final String parametersInfos) {
.build()
.toUri();
try {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(List.of(MediaType.TEXT_PLAIN));
if (StringUtils.isBlank(parametersInfos)) {
final String strUuid = restTemplate.postForObject(uri, new HttpEntity<>(new HttpHeaders()), String.class);
return UUID.fromString(strUuid);
return UUID.fromString(restTemplate.postForObject(uri, new HttpEntity<>(headers), String.class));
} else {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
final String strUuid = restTemplate.postForObject(uri, new HttpEntity<>(parametersInfos, headers), String.class);
return UUID.fromString(strUuid);
return UUID.fromString(restTemplate.postForObject(uri, new HttpEntity<>(parametersInfos, headers), String.class));
}
} catch (final HttpStatusCodeException e) {
throw handleHttpError(e, CREATE_SHORTCIRCUIT_PARAMETERS_FAILED);
Expand Down Expand Up @@ -329,12 +328,11 @@ public UUID duplicateParameters(UUID parametersUuid) {
try {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(List.of(MediaType.TEXT_PLAIN));
final String strUuid = restTemplate.postForObject(UriComponentsBuilder.fromUriString(shortCircuitServerBaseUri)
return UUID.fromString(restTemplate.postForObject(UriComponentsBuilder.fromUriString(shortCircuitServerBaseUri)
.pathSegment(SHORT_CIRCUIT_API_VERSION, "parameters")
.queryParam("duplicateFrom", parametersUuid)
.buildAndExpand()
.toUri(), new HttpEntity<>(headers), String.class);
return UUID.fromString(strUuid);
.toUri(), new HttpEntity<>(headers), String.class));
} catch (final HttpStatusCodeException e) {
throw handleHttpError(e, CREATE_SHORTCIRCUIT_PARAMETERS_FAILED);
}
Expand Down

0 comments on commit aa038ff

Please sign in to comment.