All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.helidon.microprofile.lra.StatusBodyMapper Maven / Gradle / Ivy

/*
 * Copyright (c) 2021 Oracle and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.helidon.microprofile.lra;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.Set;

import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.MessageBodyReader;
import jakarta.ws.rs.ext.MessageBodyWriter;
import org.eclipse.microprofile.lra.annotation.LRAStatus;
import org.eclipse.microprofile.lra.annotation.ParticipantStatus;

@SuppressWarnings({"unchecked", "rawtypes"})
class StatusBodyMapper implements MessageBodyReader, MessageBodyWriter> {

    private static final Set> SUPPORTED_TYPES = Set.of(
            LRAStatus.class,
            ParticipantStatus.class
    );

    @Override
    public boolean isReadable(Class type,
                              Type genericType,
                              Annotation[] annotations,
                              MediaType mediaType) {
        return SUPPORTED_TYPES.contains(type);
    }

    @Override
    public Enum readFrom(Class type,
                         Type genericType,
                         Annotation[] annotations,
                         MediaType mediaType,
                         MultivaluedMap httpHeaders,
                         InputStream entityStream) {
        String textBody = new BufferedReader(new InputStreamReader(entityStream, StandardCharsets.UTF_8))
                .lines()
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException("Empty enum value can't be mapped to " + type.getName()));

        return Enum.valueOf(type, textBody);
    }


    @Override
    public boolean isWriteable(Class type,
                               Type genericType,
                               Annotation[] annotations,
                               MediaType mediaType) {
        return SUPPORTED_TYPES.contains(type);
    }

    @Override
    public void writeTo(Enum anEnum,
                        Class type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType,
                        MultivaluedMap httpHeaders,
                        OutputStream entityStream) throws IOException, WebApplicationException {
        entityStream.write(anEnum.name().getBytes(StandardCharsets.UTF_8));
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy