Refactor pretty printing
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / jaxrs / FormattableBodyWriter.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.NonNull;
20 import org.opendaylight.restconf.server.api.FormattableBody;
21
22 abstract sealed class FormattableBodyWriter implements MessageBodyWriter<FormattableBody>
23         permits JsonFormattableBody, XmlFormattableBody {
24     @Override
25     public final boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
26             final MediaType mediaType) {
27         return FormattableBody.class.isAssignableFrom(type);
28     }
29
30     @Override
31     public final void writeTo(final FormattableBody body, final Class<?> type, final Type genericType,
32             final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders,
33             final OutputStream entityStream) throws IOException {
34         writeTo(requireNonNull(body), requireNonNull(entityStream));
35     }
36
37     abstract void writeTo(@NonNull FormattableBody body, @NonNull OutputStream out) throws IOException;
38 }