Add ApiPath.empty()
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / jaxrs / JaxRsFormattableBodyWriter.java
1 /*
2  * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.restconf.nb.jaxrs;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Type;
16 import javax.ws.rs.core.MediaType;
17 import javax.ws.rs.core.MultivaluedMap;
18 import javax.ws.rs.ext.MessageBodyWriter;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.opendaylight.restconf.api.FormatParameters;
21 import org.opendaylight.restconf.api.FormattableBody;
22
23 abstract sealed class JaxRsFormattableBodyWriter implements MessageBodyWriter<JaxRsFormattableBody>
24         permits JsonJaxRsFormattableBodyWriter, XmlJaxRsFormattableBodyWriter {
25     @Override
26     public final boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
27             final MediaType mediaType) {
28         return FormattableBody.class.isAssignableFrom(type);
29     }
30
31     @Override
32     public final void writeTo(final JaxRsFormattableBody entity, final Class<?> type, final Type genericType,
33             final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders,
34             final OutputStream entityStream) throws IOException {
35         writeTo(entity.body(), entity.format(), requireNonNull(entityStream));
36     }
37
38     @NonNullByDefault
39     abstract void writeTo(FormattableBody body, FormatParameters format, OutputStream out) throws IOException;
40 }