This commit is contained in:
Jörg Prante 2023-04-21 23:29:23 +02:00
parent fea602159f
commit 95c0501d5e
431 changed files with 31484 additions and 1 deletions

View file

@ -0,0 +1,9 @@
dependencies {
implementation 'javax.json:javax.json-api:1.1.4'
implementation 'org.apache.ws.xmlschema:xmlschema-core:2.3.0'
implementation('com.github.fge:json-schema-validator:2.2.6') {
exclude group: 'com.google.guava'
}
implementation 'org.yaml:snakeyaml:2.0'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.14.2'
}

View file

@ -0,0 +1,256 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.xbib.raml.api;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.xbib.raml.api.loader.ResourceLoader;
import org.xbib.raml.api.loader.ResourceLoaderFactories;
import org.xbib.raml.api.loader.ResourceLoaderFactory;
import org.xbib.raml.api.model.PermissiveURI;
import org.xbib.raml.api.model.common.ValidationResult;
import org.xbib.raml.api.model.v10.RamlFragment;
import org.xbib.raml.api.model.v10.api.DocumentationItem;
import org.xbib.raml.api.model.v10.api.Library;
import org.xbib.raml.api.model.v10.datamodel.ExampleSpec;
import org.xbib.raml.api.model.v10.datamodel.TypeDeclaration;
import org.xbib.raml.api.model.v10.methods.Trait;
import org.xbib.raml.api.model.v10.resources.ResourceType;
import org.xbib.raml.api.model.v10.security.SecurityScheme;
import org.xbib.raml.internal.impl.RamlBuilder;
import org.xbib.raml.internal.impl.commons.RamlHeader;
import org.xbib.raml.internal.impl.commons.model.Api;
import org.xbib.raml.internal.impl.commons.model.DefaultModelElement;
import org.xbib.raml.internal.impl.commons.model.RamlValidationResult;
import org.xbib.raml.internal.impl.commons.model.StringType;
import org.xbib.raml.internal.impl.commons.model.factory.TypeDeclarationModelFactory;
import org.xbib.raml.internal.impl.commons.nodes.RamlDocumentNode;
import org.xbib.raml.internal.utils.IOUtils;
import org.xbib.raml.internal.utils.RamlNodeUtils;
import org.xbib.raml.internal.utils.StreamUtils;
import org.xbib.raml.yagi.framework.model.DefaultModelBindingConfiguration;
import org.xbib.raml.yagi.framework.model.ModelBindingConfiguration;
import org.xbib.raml.yagi.framework.model.ModelProxyBuilder;
import org.xbib.raml.yagi.framework.nodes.ErrorNode;
import org.xbib.raml.yagi.framework.nodes.KeyValueNode;
import org.xbib.raml.yagi.framework.nodes.KeyValueNodeImpl;
import org.xbib.raml.yagi.framework.nodes.Node;
import org.xbib.raml.yagi.framework.nodes.StringNodeImpl;
/**
* Entry point class to parse top level RAML descriptors.
*/
public class RamlModelBuilder {
public static final String MODEL_PACKAGE = "org.xbib.raml.internal.impl.commons.model";
private final ResourceLoaderFactory resourceLoaderFactory;
private final RamlBuilder builder = new RamlBuilder();
public RamlModelBuilder() {
resourceLoaderFactory = ResourceLoaderFactories.defaultResourceLoaderFactory();
// new DefaultResourceLoader(RootDirectoryFileAccessGuard.fromRootDir(Sets.newHashSet("file", "http", "https"),
// ".")));
}
public RamlModelBuilder(ResourceLoader resourceLoaderFactory) {
this.resourceLoaderFactory = ResourceLoaderFactories.identityFactory(resourceLoaderFactory);
}
public RamlModelResult buildApi(String ramlLocation) {
String content = getRamlContent(ramlLocation);
if (content == null) {
return generateRamlApiResult("Raml does not exist at: " + ramlLocation);
}
return buildApi(content, ramlLocation);
}
public RamlModelResult buildApi(File ramlFile) {
String content = getRamlContent(ramlFile);
if (content == null) {
return generateRamlApiResult("Files does not exist or is not a regular file: " + ramlFile.getPath());
}
return buildApi(content, ramlFile.getPath());
}
public RamlModelResult buildApi(Reader reader, String ramlLocation) {
String content = getRamlContent(reader);
if (content == null) {
return generateRamlApiResult("Invalid reader provided with location: " + ramlLocation);
}
return buildApi(content, ramlLocation);
}
public RamlModelResult buildApi(String content, String ramlLocation) {
if (content == null) {
return buildApi(ramlLocation);
}
if (ramlLocation.matches("^[a-z]+:.*")) {
String actualName = PermissiveURI.create(ramlLocation).toString();
if (ramlLocation.startsWith("file:")) {
actualName = new File(PermissiveURI.create(ramlLocation).getPath()).getParent();
}
Node ramlNode = builder.build(content, resourceLoaderFactory.createResourceLoader(actualName), ramlLocation);
return generateRamlApiResult(ramlNode, getFragment(content));
} else {
Node ramlNode = builder.build(content, resourceLoaderFactory.createResourceLoader(Paths.get(ramlLocation).toAbsolutePath().getParent().toString()), ramlLocation);
return generateRamlApiResult(ramlNode, getFragment(content));
}
}
private RamlFragment getFragment(String content) {
try {
RamlHeader ramlHeader = RamlHeader.parse(content);
return ramlHeader.getFragment();
} catch (RamlHeader.InvalidHeaderException e) {
// ignore, already handled by builder
}
return null;
}
private RamlModelResult generateRamlApiResult(Node ramlNode, RamlFragment fragment) {
List<ValidationResult> validationResults = new ArrayList<>();
List<ErrorNode> errors = RamlNodeUtils.getErrors(ramlNode);
for (ErrorNode errorNode : errors) {
validationResults.add(new RamlValidationResult(errorNode));
}
if (validationResults.isEmpty()) {
return wrapTree(ramlNode, fragment);
}
return new RamlModelResult(validationResults);
}
private RamlModelResult generateRamlApiResult(String errorMessage) {
List<ValidationResult> validationResults = new ArrayList<>();
validationResults.add(new RamlValidationResult(errorMessage));
return new RamlModelResult(validationResults);
}
private RamlModelResult wrapTree(Node ramlNode, RamlFragment fragment) {
if (ramlNode instanceof RamlDocumentNode) {
org.xbib.raml.api.model.v10.api.Api apiV10 = ModelProxyBuilder.createModel(org.xbib.raml.api.model.v10.api.Api.class, new Api((RamlDocumentNode) ramlNode), createV10Binding());
return new RamlModelResult(apiV10);
}
if (fragment == RamlFragment.Library) {
Library library = ModelProxyBuilder.createModel(Library.class, new DefaultModelElement(ramlNode), createV10Binding());
return new RamlModelResult(library);
}
if (fragment == RamlFragment.DataType || fragment == RamlFragment.AnnotationTypeDeclaration) {
final org.xbib.raml.internal.impl.commons.model.type.TypeDeclaration delegateNode = new TypeDeclarationModelFactory().create(ramlNode);
final ModelBindingConfiguration v10Binding = createV10Binding();
TypeDeclaration typeDeclaration = ModelProxyBuilder.createModel((Class<TypeDeclaration>) v10Binding.reverseBindingOf(delegateNode), delegateNode, v10Binding);
return new RamlModelResult(typeDeclaration);
}
if (fragment == RamlFragment.DocumentationItem) {
DocumentationItem documentationItem = ModelProxyBuilder.createModel(DocumentationItem.class, new DefaultModelElement(ramlNode), createV10Binding());
return new RamlModelResult(documentationItem);
}
if (fragment == RamlFragment.SecurityScheme) {
SecurityScheme securityScheme = ModelProxyBuilder.createModel(SecurityScheme.class, new DefaultModelElement(ramlNode), createV10Binding());
return new RamlModelResult(securityScheme);
}
if (fragment == RamlFragment.Trait) {
Trait trait = ModelProxyBuilder.createModel(Trait.class, new DefaultModelElement(ramlNode), createV10Binding());
return new RamlModelResult(trait);
}
if (fragment == RamlFragment.ResourceType) {
ResourceType resourceType = ModelProxyBuilder.createModel(ResourceType.class, new DefaultModelElement(ramlNode), createV10Binding());
return new RamlModelResult(resourceType);
}
if (fragment == RamlFragment.NamedExample) {
if (!(ramlNode instanceof KeyValueNode)) {
ramlNode = new KeyValueNodeImpl(new StringNodeImpl("__NamedExample_Fragment__"), ramlNode);
}
ExampleSpec exampleSpec = ModelProxyBuilder.createModel(ExampleSpec.class, new org.xbib.raml.internal.impl.commons.model.ExampleSpec((KeyValueNode) ramlNode), createV10Binding());
return new RamlModelResult(exampleSpec);
}
throw new IllegalStateException("Invalid ramlNode type (" + ramlNode.getClass().getSimpleName() + ") or fragment (" + fragment + ") combination");
}
private ModelBindingConfiguration createV10Binding() {
final DefaultModelBindingConfiguration bindingConfiguration = new DefaultModelBindingConfiguration();
bindingConfiguration.bindPackage(MODEL_PACKAGE);
// Bind all StringTypes to the StringType implementation they are only marker interfaces
bindingConfiguration.bind(org.xbib.raml.api.model.v10.system.types.StringType.class, StringType.class);
bindingConfiguration.bind(org.xbib.raml.api.model.v10.system.types.ValueType.class, StringType.class);
bindingConfiguration.defaultTo(DefaultModelElement.class);
bindingConfiguration.bind(TypeDeclaration.class, new TypeDeclarationModelFactory());
bindingConfiguration.reverseBindPackage("org.xbib.raml.api.model.v10.datamodel");
return bindingConfiguration;
}
private String getRamlContent(File ramlFile) {
if (ramlFile == null || !ramlFile.isFile()) {
return null;
}
ResourceLoader fileLoader = resourceLoaderFactory.createResourceLoader(ramlFile.getAbsoluteFile().getParent());
return getRamlContent(ramlFile.getName(), fileLoader);
}
private String getRamlContent(Reader ramlReader) {
if (ramlReader == null) {
return null;
}
try {
return IOUtils.toString(ramlReader);
} catch (IOException e) {
return null;
} finally {
IOUtils.closeQuietly(ramlReader);
}
}
private String getRamlContent(String ramlLocation) {
if (ramlLocation == null) {
return null;
}
if (ramlLocation.startsWith("file:")) {
Path p = Paths.get(URI.create(ramlLocation)).toAbsolutePath().getParent();
return getRamlContent(ramlLocation, resourceLoaderFactory.createResourceLoader(p.toString()));
} else {
Path p = Paths.get(ramlLocation).toAbsolutePath().getParent();
return getRamlContent(ramlLocation, resourceLoaderFactory.createResourceLoader(p.toString()));
}
}
private String getRamlContent(String ramlLocation, ResourceLoader loader) {
if (ramlLocation == null) {
return null;
}
InputStream ramlStream = loader.fetchResource(ramlLocation);
if (ramlStream != null) {
return StreamUtils.toString(ramlStream);
}
return null;
}
}

View file

@ -0,0 +1,239 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.xbib.raml.api;
import java.util.ArrayList;
import java.util.List;
import org.xbib.raml.api.model.common.ValidationResult;
import org.xbib.raml.api.model.v10.RamlFragment;
import org.xbib.raml.api.model.v10.api.Api;
import org.xbib.raml.api.model.v10.api.DocumentationItem;
import org.xbib.raml.api.model.v10.api.Library;
import org.xbib.raml.api.model.v10.datamodel.ExampleSpec;
import org.xbib.raml.api.model.v10.datamodel.TypeDeclaration;
import org.xbib.raml.api.model.v10.methods.Trait;
import org.xbib.raml.api.model.v10.resources.ResourceType;
import org.xbib.raml.api.model.v10.security.SecurityScheme;
/**
* Represents the result of parsing a top level RAML descriptor or fragment.
* <p>
* If there are no parsing errors and the parsed RAML was a top level descriptor,
* the <code>Api</code> model matching the RAML version is available.
* If the parsed raml is 1.0 fragment and there are no errors, the corresponding
* fragment instance is available.
* <p>
* If there are parsing errors, the list of errors is available.
*/
public class RamlModelResult {
private List<ValidationResult> validationResults = new ArrayList<>();
private Api apiV10;
private Library library;
private TypeDeclaration typeDeclaration;
private SecurityScheme securityScheme;
private Trait trait;
private ResourceType resourceType;
private ExampleSpec exampleSpec;
private DocumentationItem documentationItem;
RamlModelResult(List<ValidationResult> validationResults) {
if (validationResults == null || validationResults.isEmpty()) {
throw new IllegalArgumentException("validationResults cannot be null or emtpy");
}
this.validationResults = validationResults;
}
RamlModelResult(Api apiV10) {
if (apiV10 == null) {
throw new IllegalArgumentException("apiV10 cannot be null");
}
this.apiV10 = apiV10;
}
RamlModelResult(Library library) {
if (library == null) {
throw new IllegalArgumentException("library cannot be null");
}
this.library = library;
}
public RamlModelResult(TypeDeclaration typeDeclaration) {
if (typeDeclaration == null) {
throw new IllegalStateException("typeDeclaration cannot be null");
}
this.typeDeclaration = typeDeclaration;
}
public RamlModelResult(SecurityScheme securityScheme) {
if (securityScheme == null) {
throw new IllegalStateException("securityScheme cannot be null");
}
this.securityScheme = securityScheme;
}
public RamlModelResult(DocumentationItem documentationItem) {
if (documentationItem == null) {
throw new IllegalStateException("documentationItem cannot be null");
}
this.documentationItem = documentationItem;
}
public RamlModelResult(Trait trait) {
if (trait == null) {
throw new IllegalStateException("trait cannot be null");
}
this.trait = trait;
}
public RamlModelResult(ResourceType resourceType) {
if (resourceType == null) {
throw new IllegalStateException("resourceType cannot be null");
}
this.resourceType = resourceType;
}
public RamlModelResult(ExampleSpec exampleSpec) {
if (exampleSpec == null) {
throw new IllegalStateException("exampleSpec cannot be null");
}
this.exampleSpec = exampleSpec;
}
/**
* @return true if any parsing error occurred
*/
public boolean hasErrors() {
return !validationResults.isEmpty();
}
/**
* @return the list of validation results if there were parsing errors
* or an empty list if there were no parsing errors
*/
public List<ValidationResult> getValidationResults() {
return validationResults;
}
/**
* @return the RAML Api v1.0 parsed without errors
* or null if there were errors or the RAML version is not 1.0 or is not a top level RAML
*/
public Api getApiV10() {
return apiV10;
}
/**
* @return the RAML Library v1.0 parsed without errors
* or null if there were errors or the RAML is not a Library fragment
*/
public Library getLibrary() {
return library;
}
/**
* @return the RAML Data Type v1.0 parsed without errors
* or null if there were errors or the RAML is not a Data Type fragment
*/
public TypeDeclaration getTypeDeclaration() {
return typeDeclaration;
}
/**
* @return the RAML Security Scheme v1.0 parsed without errors
* or null if there were errors or the RAML is not a Security Scheme fragment
*/
public SecurityScheme getSecurityScheme() {
return securityScheme;
}
/**
* @return the RAML Trait v1.0 parsed without errors
* or null if there were errors or the RAML is not a Trait fragment
*/
public Trait getTrait() {
return trait;
}
/**
* @return the RAML ResourceType v1.0 parsed without errors
* or null if there were errors or the RAML is not a ResourceType fragment
*/
public ResourceType getResourceType() {
return resourceType;
}
/**
* @return the RAML NamedExample v1.0 parsed without errors
* or null if there were errors or the RAML is not a NamedExample fragment
*/
public ExampleSpec getExampleSpec() {
return exampleSpec;
}
/**
* @return the RAML DocumentationItem v1.0 parsed without errors
* or null if there were errors or the RAML is not a DocumentationItem fragment
*/
public DocumentationItem getDocumentationItem() {
return documentationItem;
}
/**
* @return the RAML 1.0 fragment identifier or <code>null</code>
* if the RAML has errors or is version 0.8
*/
public RamlFragment getFragment() {
if (hasErrors()) {
return null;
}
if (getApiV10() != null) {
return RamlFragment.Default;
}
if (getLibrary() != null) {
return RamlFragment.Library;
}
if (getTypeDeclaration() != null) {
return RamlFragment.DataType;
}
if (getSecurityScheme() != null) {
return RamlFragment.SecurityScheme;
}
if (getTrait() != null) {
return RamlFragment.Trait;
}
if (getResourceType() != null) {
return RamlFragment.ResourceType;
}
if (getExampleSpec() != null) {
return RamlFragment.NamedExample;
}
if (getDocumentationItem() != null) {
return RamlFragment.DocumentationItem;
}
throw new IllegalStateException("Fragment not yet supported");
}
}

View file

@ -0,0 +1,84 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.xbib.raml.api.loader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.xbib.raml.internal.utils.IOUtils;
import org.xbib.raml.internal.utils.Pair;
public class CacheResourceLoader implements ResourceLoaderExtended {
private final Map<String, Pair<byte[], URI>> resources = new HashMap<>();
private final ResourceLoader resourceLoader;
public CacheResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
@Override
public InputStream fetchResource(String resourceName) {
return fetchResource(resourceName, null);
}
@Override
public InputStream fetchResource(String resourceName, ResourceUriCallback callback) {
try {
if (resources.containsKey(resourceName)) {
final byte[] resourceByteArray = resources.get(resourceName).getLeft();
final URI uriCallback = resources.get(resourceName).getRight();
if (uriCallback != null && callback != null) {
callback.onResourceFound(uriCallback);
}
return toInputStreamOrNull(resourceByteArray);
}
InputStream resource;
URI uriCallback = null;
if (resourceLoader instanceof ResourceLoaderExtended) {
resource = ((ResourceLoaderExtended) resourceLoader).fetchResource(resourceName, callback);
uriCallback = ((ResourceLoaderExtended) resourceLoader).getUriCallBackParam();
} else {
resource = resourceLoader.fetchResource(resourceName);
}
// we want to cache results even if they are null
final byte[] resourceByteArray = resource == null ? null : IOUtils.toByteArray(resource);
resources.put(resourceName, Pair.of(resourceByteArray, uriCallback));
return toInputStreamOrNull(resourceByteArray);
} catch (final IOException e) {
return resourceLoader.fetchResource(resourceName);
}
}
private ByteArrayInputStream toInputStreamOrNull(final byte[] resourceByteArray) {
return resourceByteArray == null ? null : new ByteArrayInputStream(resourceByteArray);
}
@Override
public URI getUriCallBackParam() {
if (resourceLoader != null && resourceLoader instanceof ResourceLoaderExtended) {
return ((ResourceLoaderExtended) resourceLoader).getUriCallBackParam();
}
return null;
}
}

View file

@ -0,0 +1,90 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.xbib.raml.api.loader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
public class ClassPathResourceLoader implements ResourceLoaderExtended {
private final String rootRamlPackage;
private URI callbackParam;
public ClassPathResourceLoader() {
rootRamlPackage = "";
}
/**
* When the root raml is loaded through the classpath allows specifying
* the package where it is loaded from in order to correctly resolve
* absolute path includes and libraries
*
* @param rootRamlPackage in the classpath
*/
public ClassPathResourceLoader(String rootRamlPackage) {
if (rootRamlPackage.isBlank() || rootRamlPackage.equals("/")) {
this.rootRamlPackage = "";
} else {
this.rootRamlPackage = (rootRamlPackage.startsWith("/") ? rootRamlPackage.substring(1) : rootRamlPackage) + "/";
}
}
private URL getResource(String uselessRootPackage, String resourceName) {
String fixedResourceName = uselessRootPackage + (resourceName.startsWith("/") ? resourceName.substring(1) : resourceName);
URL url = getClass().getClassLoader().getResource(fixedResourceName);
if (url == null) {
return Thread.currentThread().getContextClassLoader().getResource(fixedResourceName);
}
return url;
}
@Override
public InputStream fetchResource(String resourceName, ResourceUriCallback callback) {
try {
final URL url = getResource(rootRamlPackage, resourceName);
if (url != null) {
this.callbackParam = url.toURI();
if (callback != null) {
callback.onResourceFound(callbackParam);
}
return url.openStream();
}
return null;
} catch (IOException | URISyntaxException e) {
return null;
}
}
@Override
public InputStream fetchResource(String resourceName) {
return fetchResource(resourceName, null);
}
@Override
public URI getUriCallBackParam() {
return callbackParam;
}
}

View file

@ -0,0 +1,64 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.xbib.raml.api.loader;
import java.io.InputStream;
import java.net.URI;
public class CompositeResourceLoader implements ResourceLoaderExtended {
private final ResourceLoader[] resourceLoaders;
private ResourceLoader callBackLoader;
public CompositeResourceLoader(ResourceLoader... resourceLoaders) {
this.resourceLoaders = resourceLoaders;
}
public static CompositeResourceLoader compose(ResourceLoader... resourceLoaders) {
return new CompositeResourceLoader(resourceLoaders);
}
@Override
public InputStream fetchResource(String resourceName, ResourceUriCallback callback) {
InputStream inputStream = null;
for (ResourceLoader loader : resourceLoaders) {
if (loader instanceof ResourceLoaderExtended) {
inputStream = ((ResourceLoaderExtended) loader).fetchResource(resourceName, callback);
} else {
inputStream = loader.fetchResource(resourceName);
}
if (inputStream != null) {
callBackLoader = loader;
break;
}
}
return inputStream;
}
@Override
public InputStream fetchResource(String resourceName) {
return fetchResource(resourceName, null);
}
@Override
public URI getUriCallBackParam() {
if (callBackLoader != null && callBackLoader instanceof ResourceLoaderExtended) {
return ((ResourceLoaderExtended) callBackLoader).getUriCallBackParam();
}
return null;
}
}

View file

@ -0,0 +1,54 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.xbib.raml.api.loader;
import java.io.InputStream;
import java.net.URI;
public class DefaultResourceLoader implements ResourceLoaderExtended {
private final ResourceLoader resourceLoader;
public DefaultResourceLoader() {
resourceLoader = CompositeResourceLoader.compose(
new UrlResourceLoader(),
new RamlUrlResourceLoader(),
new ClassPathResourceLoader(),
new FileResourceLoader("."));
}
@Override
public InputStream fetchResource(String resourceName, ResourceUriCallback callback) {
if (resourceLoader instanceof ResourceLoaderExtended) {
return ((ResourceLoaderExtended) resourceLoader).fetchResource(resourceName, callback);
} else {
return resourceLoader.fetchResource(resourceName);
}
}
@Override
public InputStream fetchResource(String resourceName) {
return fetchResource(resourceName, null);
}
@Override
public URI getUriCallBackParam() {
if (resourceLoader != null && resourceLoader instanceof ResourceLoaderExtended) {
return ((ResourceLoaderExtended) resourceLoader).getUriCallBackParam();
}
return null;
}
}

View file

@ -0,0 +1,65 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.xbib.raml.api.loader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
public class FileResourceLoader implements ResourceLoaderExtended {
private final File parentPath;
private URI callbackParam;
public FileResourceLoader(String path) {
this(new File(path));
}
public FileResourceLoader(File path) {
this.parentPath = path;
}
@Override
public InputStream fetchResource(String resourceName, ResourceUriCallback callback) {
File includedFile = new File(resourceName);
if (!includedFile.isAbsolute()) {
includedFile = new File(parentPath, resourceName);
}
try {
if (callback != null) {
callbackParam = includedFile.toURI();
callback.onResourceFound(callbackParam);
}
return new FileInputStream(includedFile);
} catch (IOException e) {
return null;
}
}
@Override
public InputStream fetchResource(String resourceName) {
return fetchResource(resourceName, null);
}
@Override
public URI getUriCallBackParam() {
return callbackParam;
}
}

View file

@ -0,0 +1,27 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.xbib.raml.api.loader;
import java.io.IOException;
import java.io.InputStream;
/**
* Created. There, you have it.
*/
public interface InputStreamProvider<T> {
InputStream apply(T object) throws IOException;
}

View file

@ -0,0 +1,59 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.xbib.raml.api.loader;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
class RamlUrlResourceLoader implements ResourceLoaderExtended {
public static final String APPLICATION_RAML = "application/raml+yaml";
private URI callbackParam;
@Override
public InputStream fetchResource(String resourceName, ResourceUriCallback callback) {
try {
URL url = new URL(resourceName);
URLConnection connection = url.openConnection();
connection.setRequestProperty("Accept", APPLICATION_RAML + ", */*");
if (callback != null) {
callbackParam = url.toURI();
callback.onResourceFound(callbackParam);
}
return new BufferedInputStream(connection.getInputStream());
} catch (IOException e) {
// ignore on resource not found
} catch (URISyntaxException e) {
// Ignore
}
return null;
}
@Override
public InputStream fetchResource(String resourceName) {
return fetchResource(resourceName, null);
}
@Override
public URI getUriCallBackParam() {
return callbackParam;
}
}

View file

@ -0,0 +1,31 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.xbib.raml.api.loader;
import java.io.InputStream;
public interface ResourceLoader {
/**
* Returns an input stream for reading the specified resource.
*
* @param resourceName the resource to try to fetch
* @return An input stream for reading the resource, or <tt>null</tt>
* if the resource could not be found
*/
InputStream fetchResource(String resourceName);
}

View file

@ -0,0 +1,22 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.xbib.raml.api.loader;
public interface ResourceLoaderAware {
void setResourceLoader(ResourceLoader resourceLoader);
}

View file

@ -0,0 +1,37 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.xbib.raml.api.loader;
import java.io.InputStream;
import java.net.URI;
public interface ResourceLoaderExtended extends ResourceLoader {
/**
* Returns an input stream for reading the specified resource.
*
* @param resourceName the resource to try to fetch
* @return An input stream for reading the resource, or <tt>null</tt>
* if the resource could not be found
*/
InputStream fetchResource(String resourceName, ResourceUriCallback callback);
/**
* Returns the URI used by ResourceUriCallback if exists
*
* @return An URI if callback is called in fetchResource method, or <tt>null</tt>
*/
URI getUriCallBackParam();
}

View file

@ -0,0 +1,65 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.xbib.raml.api.loader;
import java.io.File;
import java.io.InputStream;
/**
* Created. There, you have it.
*/
public class ResourceLoaderFactories {
// this continously returns the same resource loader.
public static ResourceLoaderFactory identityFactory(final ResourceLoader loader) {
return new ResourceLoaderFactory() {
@Override
public ResourceLoader createResourceLoader(String parent) {
return loader;
}
};
}
// this is used nowhere but in the empty positions. these loaders shouldn't open files.
public static ResourceLoaderFactory nullFactory() {
return new ResourceLoaderFactory() {
@Override
public ResourceLoader createResourceLoader(String parent) {
return new ResourceLoader() {
@Override
public InputStream fetchResource(String resourceName) {
return null;
}
};
}
};
}
public static ResourceLoaderFactory defaultResourceLoaderFactory() {
return new ResourceLoaderFactory() {
@Override
public ResourceLoader createResourceLoader(String parent) {
return CompositeResourceLoader.compose(
new DefaultResourceLoader(),
new RootRamlFileResourceLoader(new File(parent)),
new RootRamlUrlResourceLoader(parent)
);
}
};
}
}

View file

@ -0,0 +1,23 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.xbib.raml.api.loader;
/**
* Created. There, you have it.
*/
public interface ResourceLoaderFactory {
ResourceLoader createResourceLoader(String parent);
}

View file

@ -0,0 +1,22 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.xbib.raml.api.loader;
import java.net.URI;
public interface ResourceUriCallback {
void onResourceFound(URI resourceURI);
}

View file

@ -0,0 +1,62 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.xbib.raml.api.loader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;
public class RootRamlFileResourceLoader implements ResourceLoaderExtended {
private final Logger logger = Logger.getLogger(RootRamlFileResourceLoader.class.getName());
private final File parentPath;
private URI callbackParam;
public RootRamlFileResourceLoader(File path) {
this.parentPath = path;
}
@Override
public InputStream fetchResource(String resourceName, ResourceUriCallback callback) {
final File includedFile = new File(parentPath, resourceName.startsWith("/") ? resourceName.substring(1) : resourceName);
logger.log(Level.FINE, "Looking for resource " + resourceName + " on directory " + parentPath);
try {
if (callback != null) {
callbackParam = includedFile.toURI();
callback.onResourceFound(callbackParam);
}
return new FileInputStream(includedFile);
} catch (IOException e) {
return null;
}
}
@Override
public InputStream fetchResource(String resourceName) {
return fetchResource(resourceName, null);
}
@Override
public URI getUriCallBackParam() {
return callbackParam;
}
}

View file

@ -0,0 +1,64 @@
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,