2301c2b2f4f543e1f21ec0c4c4e251dec1da3dcf
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / databind / AbstractBody.java
1 /*
2  * Copyright (c) 2023 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.rfc8040.databind;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.lang.invoke.MethodHandles;
15 import java.lang.invoke.VarHandle;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
19 import org.opendaylight.restconf.common.errors.RestconfError;
20 import org.opendaylight.yangtools.yang.data.api.YangNetconfError;
21 import org.opendaylight.yangtools.yang.data.api.YangNetconfErrorAware;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * An abstract request body backed by an {@link InputStream}.
27  */
28 public abstract sealed class AbstractBody implements AutoCloseable
29         permits ChildBody, DataPostBody, OperationInputBody, PatchBody, ResourceBody {
30     private static final Logger LOG = LoggerFactory.getLogger(AbstractBody.class);
31
32     private static final VarHandle INPUT_STREAM;
33
34     static {
35         try {
36             INPUT_STREAM = MethodHandles.lookup().findVarHandle(AbstractBody.class, "inputStream", InputStream.class);
37         } catch (NoSuchFieldException | IllegalAccessException e) {
38             throw new ExceptionInInitializerError(e);
39         }
40     }
41
42     @SuppressWarnings("unused")
43     private volatile InputStream inputStream;
44
45     AbstractBody(final InputStream inputStream) {
46         this.inputStream = requireNonNull(inputStream);
47     }
48
49     @Override
50     public final void close() {
51         final var is = getStream();
52         if (is != null) {
53             try {
54                 is.close();
55             } catch (IOException e) {
56                 LOG.info("Failed to close input", e);
57             }
58         }
59     }
60
61     final @NonNull InputStream acquireStream() {
62         final var is = getStream();
63         if (is == null) {
64             throw new IllegalStateException("Input stream has already been consumed");
65         }
66         return is;
67     }
68
69
70     /**
71      * Throw a {@link RestconfDocumentedException} if the specified exception has a {@link YangNetconfError} attachment.
72      *
73      * @param cause Proposed cause of a RestconfDocumentedException
74      */
75     static void throwIfYangError(final Exception cause) {
76         if (cause instanceof YangNetconfErrorAware infoAware) {
77             throw new RestconfDocumentedException(cause, infoAware.getNetconfErrors().stream()
78                 .map(error -> new RestconfError(error.type(), error.tag(), error.message(), error.appTag(),
79                     // FIXME: pass down error info
80                     null, error.path()))
81                 .toList());
82         }
83     }
84
85     private @Nullable InputStream getStream() {
86         return (InputStream) INPUT_STREAM.getAndSet(this, null);
87     }
88 }