All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.swagger.parser.util.RemoteUrl Maven / Gradle / Ivy

There is a newer version: 2.0.0-rc1
Show newest version
package io.swagger.parser.util;

import com.wordnik.swagger.models.auth.AuthorizationValue;

import java.net.*;
import java.io.*;
import java.util.*;

public class RemoteUrl {
  public static String urlToString(String url, List auths) throws Exception {
    InputStream is = null;
    URLConnection conn = null;
    BufferedReader br = null;

    try{
      if(auths != null) {
        StringBuilder queryString = new StringBuilder();
        // build a new url if needed
        for(AuthorizationValue auth: auths) {
          if("query".equals(auth.getType())) {
            if(queryString.toString().length() == 0)
              queryString.append("?");
            else
              queryString.append("&");
            queryString.append(URLEncoder.encode(auth.getKeyName(), "UTF-8"))
              .append("=")
              .append(URLEncoder.encode(auth.getValue(), "UTF-8"));
          }
        }
        if(queryString.toString().length() != 0)
          url = url + queryString.toString();
        conn = new URL(url).openConnection();
        for(AuthorizationValue auth: auths) {
          if("header".equals(auth.getType())) {
            conn.setRequestProperty(auth.getKeyName(), auth.getValue());
          }
        }
      }
      else {
        conn = new URL(url).openConnection();
      }

      StringBuilder sb = new StringBuilder();
   
      String line;
      is = conn.getInputStream();
      br = new BufferedReader(new InputStreamReader(is));
      while ((line = br.readLine()) != null) {
        sb.append(line);
      }
      return sb.toString();
    }
    catch (javax.net.ssl.SSLProtocolException e){
      System.out.println("there is a problem with the target SSL certificate");
      System.out.println("**** you may want to run with -Djsse.enableSNIExtension=false\n\n");
      e.printStackTrace();
      throw e;
    }
    catch (Exception e) {
      e.printStackTrace();
      throw e;
    }
    finally {
      if(is != null) 
        is.close();
      if(br != null)
        br.close();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy