73e40c9079e3096fe2667ded75e0c011466d161f
[netconf.git] / restconf / restconf-openapi / src / main / java / org / opendaylight / restconf / openapi / impl / SecuritySchemesStream.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.openapi.impl;
9
10 import java.io.BufferedReader;
11 import java.io.ByteArrayInputStream;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.io.InputStreamReader;
15 import java.io.Reader;
16 import java.nio.charset.StandardCharsets;
17 import java.util.Map;
18 import org.opendaylight.restconf.openapi.jaxrs.OpenApiBodyWriter;
19 import org.opendaylight.restconf.openapi.model.OpenApiEntity;
20 import org.opendaylight.restconf.openapi.model.SecuritySchemesEntity;
21 import org.opendaylight.restconf.openapi.model.security.SecuritySchemeObject;
22
23 public final class SecuritySchemesStream extends InputStream {
24     private final OpenApiBodyWriter writer;
25     private final SecuritySchemesEntity securitySchemesEntity;
26
27     private Reader reader;
28
29     public SecuritySchemesStream(final OpenApiBodyWriter writer,
30             final Map<String, SecuritySchemeObject> securitySchemes) {
31         this.writer = writer;
32         this.securitySchemesEntity = new SecuritySchemesEntity(securitySchemes);
33     }
34
35     @Override
36     public int read() throws IOException {
37         if (reader == null) {
38             reader = new BufferedReader(
39                 new InputStreamReader(new ByteArrayInputStream(writeNextEntity(securitySchemesEntity)),
40                     StandardCharsets.UTF_8));
41         }
42         return reader.read();
43     }
44
45     @Override
46     public int read(final byte[] array, final int off, final int len) throws IOException {
47         return super.read(array, off, len);
48     }
49
50     private byte[] writeNextEntity(final OpenApiEntity next) throws IOException {
51         writer.writeTo(next, null, null, null, null, null, null);
52         return writer.readFrom();
53     }
54 }