Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.rest; |
| 17 | |
| 18 | import com.google.common.annotations.Beta; |
| 19 | import org.slf4j.Logger; |
| 20 | import org.slf4j.LoggerFactory; |
| 21 | |
| 22 | import java.io.InputStream; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | |
| 26 | /** |
| 27 | * Entity capable of providing REST API documentation resources. |
| 28 | */ |
| 29 | @Beta |
| 30 | public class ApiDocProvider { |
| 31 | |
| 32 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 33 | |
| 34 | private static final String DOCS = "/apidoc/swagger.json"; |
| 35 | private static final String MODEL = "/apidoc/model.json"; |
| 36 | |
| 37 | private final String key; |
| 38 | private final String name; |
| 39 | private final ClassLoader classLoader; |
| 40 | |
| 41 | /** |
| 42 | * Creates a new REST API documentation provider. |
| 43 | * |
| 44 | * @param key REST API key |
| 45 | * @param name REST API name |
| 46 | * @param classLoader class loader |
| 47 | */ |
| 48 | public ApiDocProvider(String key, String name, ClassLoader classLoader) { |
| 49 | this.key = checkNotNull(key, "Key cannot be null"); |
| 50 | this.name = checkNotNull(name, "Name cannot be null"); |
| 51 | this.classLoader = checkNotNull(classLoader, "Class loader cannot be null"); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns the REST API key. |
| 56 | * |
| 57 | * @return REST API key |
| 58 | */ |
| 59 | public String key() { |
| 60 | return key; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns the REST API name. |
| 65 | * |
| 66 | * @return REST API name |
| 67 | */ |
| 68 | public String name() { |
| 69 | return name; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns input stream containing Swagger UI compatible JSON. |
| 74 | * |
| 75 | * @return input stream with Swagger JSON data |
| 76 | */ |
| 77 | public InputStream docs() { |
| 78 | return get(DOCS); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns input stream containing JSON model schema. |
| 83 | * |
| 84 | * @return input stream with JSON model schema |
| 85 | */ |
| 86 | public InputStream model() { |
| 87 | return get(MODEL); |
| 88 | } |
| 89 | |
| 90 | private InputStream get(String resource) { |
| 91 | InputStream stream = classLoader.getResourceAsStream(resource); |
| 92 | if (stream == null) { |
| 93 | log.warn("Unable to find JSON resource {}", resource); |
| 94 | } |
| 95 | return stream; |
| 96 | } |
| 97 | |
| 98 | } |