use of io.swagger.models.auth.AuthorizationValue in project swagger-parser by swagger-api.
the class RemoteUrl method urlToString.
public static String urlToString(String url, List<AuthorizationValue> auths) throws Exception {
InputStream is = null;
BufferedReader br = null;
try {
final URL inUrl = new URL(cleanUrl(url));
final List<AuthorizationValue> query = new ArrayList<>();
final List<AuthorizationValue> header = new ArrayList<>();
if (auths != null) {
for (AuthorizationValue auth : auths) {
if ("query".equals(auth.getType())) {
appendValue(inUrl, auth, query);
} else if ("header".equals(auth.getType())) {
appendValue(inUrl, auth, header);
}
}
}
final URLConnection conn;
if (!query.isEmpty()) {
final URI inUri = inUrl.toURI();
final StringBuilder newQuery = new StringBuilder(inUri.getQuery() == null ? "" : inUri.getQuery());
for (AuthorizationValue item : query) {
if (newQuery.length() > 0) {
newQuery.append("&");
}
newQuery.append(URLEncoder.encode(item.getKeyName(), UTF_8.name())).append("=").append(URLEncoder.encode(item.getValue(), UTF_8.name()));
}
conn = new URI(inUri.getScheme(), inUri.getAuthority(), inUri.getPath(), newQuery.toString(), inUri.getFragment()).toURL().openConnection();
} else {
conn = inUrl.openConnection();
}
CONNECTION_CONFIGURATOR.process(conn);
for (AuthorizationValue item : header) {
conn.setRequestProperty(item.getKeyName(), item.getValue());
}
conn.setRequestProperty("Accept", ACCEPT_HEADER_VALUE);
conn.setRequestProperty("User-Agent", USER_AGENT_HEADER_VALUE);
conn.connect();
InputStream in = conn.getInputStream();
StringBuilder contents = new StringBuilder();
BufferedReader input = new BufferedReader(new InputStreamReader(in, UTF_8));
for (int i = 0; i != -1; i = input.read()) {
char c = (char) i;
if (!Character.isISOControl(c)) {
contents.append((char) i);
}
if (c == '\n') {
contents.append('\n');
}
}
in.close();
return contents.toString();
} catch (javax.net.ssl.SSLProtocolException e) {
LOGGER.warn("there is a problem with the target SSL certificate");
LOGGER.warn("**** you may want to run with -Djsse.enableSNIExtension=false\n\n");
LOGGER.error("unable to read", e);
throw e;
} catch (Exception e) {
LOGGER.error("unable to read", e);
throw e;
} finally {
if (is != null) {
is.close();
}
if (br != null) {
br.close();
}
}
}
use of io.swagger.models.auth.AuthorizationValue in project swagger-parser by swagger-api.
the class NetworkReferenceTests method testIssue328.
@Test
public void testIssue328() throws Exception {
new Expectations() {
{
remoteUrl.urlToString("http://localhost:8080/resources/swagger/issue-328.yaml", new ArrayList<AuthorizationValue>());
result = issue_328_yaml;
remoteUrl.urlToString("http://localhost:8080/resources/swagger/issue-328-events.yaml", new ArrayList<AuthorizationValue>());
result = issue_328_events_yaml;
remoteUrl.urlToString("http://localhost:8080/resources/swagger/common/issue-328-paging.yaml", new ArrayList<AuthorizationValue>());
result = issue_328_paging_yaml;
remoteUrl.urlToString("http://localhost:8080/resources/swagger/common/common2/issue-328-bar.yaml", new ArrayList<AuthorizationValue>());
result = issue_328_bar_yaml;
}
};
SwaggerDeserializationResult result = new SwaggerParser().readWithInfo("http://localhost:8080/resources/swagger/issue-328.yaml", null, true);
assertNotNull(result.getSwagger());
Swagger swagger = result.getSwagger();
assertNotNull(swagger.getPath("/events"));
assertNotNull(swagger.getDefinitions().get("StatusResponse"));
assertNotNull(swagger.getDefinitions().get("Paging"));
assertNotNull(swagger.getDefinitions().get("Foobar"));
}
use of io.swagger.models.auth.AuthorizationValue in project swagger-parser by swagger-api.
the class NetworkReferenceTests method testIssue330.
@Test
public void testIssue330() throws Exception {
new Expectations() {
{
remoteUrl.urlToString("http://server1/resources/swagger.yaml", new ArrayList<AuthorizationValue>());
result = issue_330_yaml;
remoteUrl.urlToString("http://server1/resources/common/paging.yaml", new ArrayList<AuthorizationValue>());
result = issue_330_paging_yaml;
remoteUrl.urlToString("http://server1/resources/common/users.yaml", new ArrayList<AuthorizationValue>());
result = issue_330_users_yaml;
remoteUrl.urlToString("http://server2/resources/common/entities.yaml", new ArrayList<AuthorizationValue>());
result = issue_330_entities_yaml;
}
};
SwaggerDeserializationResult result = new SwaggerParser().readWithInfo("http://server1/resources/swagger.yaml", null, true);
assertNotNull(result.getSwagger());
Swagger swagger = result.getSwagger();
assertNotNull(swagger.getPath("/events"));
assertNotNull(swagger.getDefinitions().get("Address"));
assertNotNull(swagger.getDefinitions().get("Paging"));
assertNotNull(swagger.getDefinitions().get("users"));
assertNotNull(swagger.getDefinitions().get("Phone"));
}
use of io.swagger.models.auth.AuthorizationValue in project swagger-parser by swagger-api.
the class RemoteUrlTest method testSkippedHeader.
@Test
public void testSkippedHeader() throws Exception {
final String expectedBody = setupStub();
final String headerName = "Authorization";
final String headerValue = "foobar";
final AuthorizationValue authorizationValue = new HostAuthorizationValue(SOME_HOST, headerName, headerValue, "header");
final String actualBody = RemoteUrl.urlToString(getUrl(), Arrays.asList(authorizationValue));
assertEquals(actualBody, expectedBody);
verify(getRequestedFor(urlEqualTo("/v2/pet/1")).withHeader("Accept", equalTo(EXPECTED_ACCEPTS_HEADER)).withoutHeader(headerName));
}
use of io.swagger.models.auth.AuthorizationValue in project swagger-parser by swagger-api.
the class RemoteUrlTest method testAuthorizationHeader.
@Test
public void testAuthorizationHeader() throws Exception {
final String expectedBody = setupStub();
final String headerName = "Authorization";
final String headerValue = "foobar";
final AuthorizationValue authorizationValue = new AuthorizationValue(headerName, headerValue, "header");
final String actualBody = RemoteUrl.urlToString(getUrl(), Arrays.asList(authorizationValue));
assertEquals(actualBody, expectedBody);
verify(getRequestedFor(urlEqualTo("/v2/pet/1")).withHeader("Accept", equalTo(EXPECTED_ACCEPTS_HEADER)).withHeader(headerName, equalTo(headerValue)));
}
Aggregations