-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b19a4ac
commit aa038ff
Showing
4 changed files
with
119 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/main/java/org/gridsuite/study/server/converter/UuidHttpConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters