1 /*
2 * #%L
3 * wcm.io
4 * %%
5 * Copyright (C) 2015 wcm.io
6 * %%
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * #L%
19 */
20 package io.wcm.caravan.hal.docs.impl;
21
22 import io.wcm.caravan.hal.docs.impl.reader.ServiceModelReader;
23 import io.wcm.caravan.jaxrs.publisher.ApplicationPath;
24
25 import org.osgi.framework.Bundle;
26
27 /**
28 * Build the documentation base URI.
29 */
30 public final class DocsPath {
31
32 /**
33 * Prefix for documentation URIs
34 */
35 static final String DOCS_URI_PREFIX = "/docs/api";
36
37 private DocsPath() {
38 // static methods only
39 }
40
41 /**
42 * Gets documentation URI path.
43 * @param bundle Bundle
44 * @return Path or null if not a JAX-RS application bundle or no haldocs metadata exist.
45 */
46 public static String get(Bundle bundle) {
47 String applicationPath = ApplicationPath.get(bundle);
48 if (applicationPath == null) {
49 return null;
50 }
51 if (!hasHalDocs(bundle)) {
52 return null;
53 }
54 return get(applicationPath);
55 }
56
57 /**
58 * Gets documentation URI path
59 * @param applicationPath JAX-RS application path
60 * @return Path
61 */
62 public static String get(String applicationPath) {
63 return DOCS_URI_PREFIX + applicationPath;
64 }
65
66 /**
67 * Checks if the given bundle has documentation metadata stored as resource.
68 * @param bundle Bundle
69 * @return true if metadata found
70 */
71 public static boolean hasHalDocs(Bundle bundle) {
72 return bundle.getResource(ServiceModelReader.DOCS_CLASSPATH_PREFIX + "/" + ServiceModelReader.SERVICE_DOC_FILE) != null;
73 }
74
75 }