@Test public void testReadExternalRef_InternalFormat(@Injectable final List<AuthorizationValue> auths) throws Exception { final String file = "#/defintiions/foo"; try { RefUtils.readExternalRef(file, RefFormat.INTERNAL, auths, null); fail("Should have thrown an exception"); } catch (RuntimeException e) { } }
public static String readExternalRef(String file, RefFormat refFormat, List<AuthorizationValue> auths, Path parentDirectory) { if (!RefUtils.isAnExternalRefFormat(refFormat)) { throw new RuntimeException("Ref is not external"); } String result = null; try { if (refFormat == RefFormat.URL) { result = RemoteUrl.urlToString(file, auths); } else { final Path pathToUse = parentDirectory.resolve(file).normalize(); if(Files.exists(pathToUse)) { result = readAll(pathToUse); } else { String url = file; if (url.contains("..")) { int parentCount = 0; while (url.contains("..")) { url = url.substring(url.indexOf(".") + 2); parentCount++; } for (int i = 0; i < parentCount - 1; i++) { parentDirectory = parentDirectory.getParent(); } url = parentDirectory + url; } else { url = parentDirectory + url.substring(url.indexOf(".") + 1); } final Path pathToUse2 = parentDirectory.resolve(url).normalize(); if(Files.exists(pathToUse2)) { result = readAll(pathToUse2); } } if (result == null){ result = ClasspathHelper.loadFileFromClasspath(file); } } } catch (Exception e) { throw new RuntimeException("Unable to load " + refFormat + " ref: " + file + " path: "+parentDirectory, e); } return result; }
RefUtils { public static String readExternalRef(String file, RefFormat refFormat, List<AuthorizationValue> auths, Path parentDirectory) { if (!RefUtils.isAnExternalRefFormat(refFormat)) { throw new RuntimeException("Ref is not external"); } String result = null; try { if (refFormat == RefFormat.URL) { result = RemoteUrl.urlToString(file, auths); } else { final Path pathToUse = parentDirectory.resolve(file).normalize(); if(Files.exists(pathToUse)) { result = readAll(pathToUse); } else { String url = file; if (url.contains("..")) { int parentCount = 0; while (url.contains("..")) { url = url.substring(url.indexOf(".") + 2); parentCount++; } for (int i = 0; i < parentCount - 1; i++) { parentDirectory = parentDirectory.getParent(); } url = parentDirectory + url; } else { url = parentDirectory + url.substring(url.indexOf(".") + 1); } final Path pathToUse2 = parentDirectory.resolve(url).normalize(); if(Files.exists(pathToUse2)) { result = readAll(pathToUse2); } } if (result == null){ result = ClasspathHelper.loadFileFromClasspath(file); } } } catch (Exception e) { throw new RuntimeException("Unable to load " + refFormat + " ref: " + file + " path: "+parentDirectory, e); } return result; } }
RefUtils { public static String readExternalRef(String file, RefFormat refFormat, List<AuthorizationValue> auths, Path parentDirectory) { if (!RefUtils.isAnExternalRefFormat(refFormat)) { throw new RuntimeException("Ref is not external"); } String result = null; try { if (refFormat == RefFormat.URL) { result = RemoteUrl.urlToString(file, auths); } else { final Path pathToUse = parentDirectory.resolve(file).normalize(); if(Files.exists(pathToUse)) { result = readAll(pathToUse); } else { String url = file; if (url.contains("..")) { int parentCount = 0; while (url.contains("..")) { url = url.substring(url.indexOf(".") + 2); parentCount++; } for (int i = 0; i < parentCount - 1; i++) { parentDirectory = parentDirectory.getParent(); } url = parentDirectory + url; } else { url = parentDirectory + url.substring(url.indexOf(".") + 1); } final Path pathToUse2 = parentDirectory.resolve(url).normalize(); if(Files.exists(pathToUse2)) { result = readAll(pathToUse2); } } if (result == null){ result = ClasspathHelper.loadFileFromClasspath(file); } } } catch (Exception e) { throw new RuntimeException("Unable to load " + refFormat + " ref: " + file + " path: "+parentDirectory, e); } return result; } private  RefUtils(); }
RefUtils { public static String readExternalRef(String file, RefFormat refFormat, List<AuthorizationValue> auths, Path parentDirectory) { if (!RefUtils.isAnExternalRefFormat(refFormat)) { throw new RuntimeException("Ref is not external"); } String result = null; try { if (refFormat == RefFormat.URL) { result = RemoteUrl.urlToString(file, auths); } else { final Path pathToUse = parentDirectory.resolve(file).normalize(); if(Files.exists(pathToUse)) { result = readAll(pathToUse); } else { String url = file; if (url.contains("..")) { int parentCount = 0; while (url.contains("..")) { url = url.substring(url.indexOf(".") + 2); parentCount++; } for (int i = 0; i < parentCount - 1; i++) { parentDirectory = parentDirectory.getParent(); } url = parentDirectory + url; } else { url = parentDirectory + url.substring(url.indexOf(".") + 1); } final Path pathToUse2 = parentDirectory.resolve(url).normalize(); if(Files.exists(pathToUse2)) { result = readAll(pathToUse2); } } if (result == null){ result = ClasspathHelper.loadFileFromClasspath(file); } } } catch (Exception e) { throw new RuntimeException("Unable to load " + refFormat + " ref: " + file + " path: "+parentDirectory, e); } return result; } private  RefUtils(); static String computeDefinitionName(String ref); static Optional<String> getExternalPath(String ref); static boolean isAnExternalRefFormat(RefFormat refFormat); static RefFormat computeRefFormat(String ref); static String mungedRef(String refString); static String readExternalUrlRef(String file, RefFormat refFormat, List<AuthorizationValue> auths,
                                            String rootPath); static String readExternalClasspathRef(String file, RefFormat refFormat, List<AuthorizationValue> auths,
                                                  String rootPath); static String buildUrl(String rootPath, String relativePath); static String readExternalRef(String file, RefFormat refFormat, List<AuthorizationValue> auths,
                                         Path parentDirectory); }
RefUtils { public static String readExternalRef(String file, RefFormat refFormat, List<AuthorizationValue> auths, Path parentDirectory) { if (!RefUtils.isAnExternalRefFormat(refFormat)) { throw new RuntimeException("Ref is not external"); } String result = null; try { if (refFormat == RefFormat.URL) { result = RemoteUrl.urlToString(file, auths); } else { final Path pathToUse = parentDirectory.resolve(file).normalize(); if(Files.exists(pathToUse)) { result = readAll(pathToUse); } else { String url = file; if (url.contains("..")) { int parentCount = 0; while (url.contains("..")) { url = url.substring(url.indexOf(".") + 2); parentCount++; } for (int i = 0; i < parentCount - 1; i++) { parentDirectory = parentDirectory.getParent(); } url = parentDirectory + url; } else { url = parentDirectory + url.substring(url.indexOf(".") + 1); } final Path pathToUse2 = parentDirectory.resolve(url).normalize(); if(Files.exists(pathToUse2)) { result = readAll(pathToUse2); } } if (result == null){ result = ClasspathHelper.loadFileFromClasspath(file); } } } catch (Exception e) { throw new RuntimeException("Unable to load " + refFormat + " ref: " + file + " path: "+parentDirectory, e); } return result; } private  RefUtils(); static String computeDefinitionName(String ref); static Optional<String> getExternalPath(String ref); static boolean isAnExternalRefFormat(RefFormat refFormat); static RefFormat computeRefFormat(String ref); static String mungedRef(String refString); static String readExternalUrlRef(String file, RefFormat refFormat, List<AuthorizationValue> auths,
                                            String rootPath); static String readExternalClasspathRef(String file, RefFormat refFormat, List<AuthorizationValue> auths,
                                                  String rootPath); static String buildUrl(String rootPath, String relativePath); static String readExternalRef(String file, RefFormat refFormat, List<AuthorizationValue> auths,
                                         Path parentDirectory);  }