A Tutorial for Real Time Pitch Detection with HTML5

This post has been moved to .

Advertisements

Three Ways to Access the JIRA API

Here is a list of various urls and commands that are helpful for accessing JIRA.

The -k is for insecure connections, and -u is to add the username:password to each command. You might need the $projectKey or $issueID as well.

HttpClient


	public static void main(String[] args) throws Exception {
		System.out.println("Step 1: Setup Authentication Information");
		String HOST = jlc.getHost(); // jira.corp.com
		String URL = jlc.getUrl(); // https://jira.corp.com
		String USERNAME = jlc.getUsername(); // ...
		String PASSWORD = jlc.getPassword(); // ...

		System.out.println("Step 2: Install SSL Certificate");		
		SSLHandler.setupSSLContextByInstallingCertification(HOST);

		System.out.println("Step 3: Query Server");	
		HttpGet httpGet = new HttpGet(URL+"/rest/api/2/project");
		String basic_auth = new String(Base64.encodeBase64((USERNAME+":"+PASSWORD).getBytes()));
		httpGet.addHeader("Authorization", "Basic " + basic_auth);
		
		CloseableHttpResponse response = new DefaultHttpClient().execute(httpGet);
		String jsonResponse = EntityUtils.toString(response.getEntity());
		response.close();

		System.out.println("Step 4: List Projects");
		JSONArray projectArray = new JSONArray(jsonResponse);
		for (int i = 0; i < projectArray.length(); i++) {
			JSONObject proj = projectArray.getJSONObject(i);
			System.out.println("JSONObject Project Name:"+proj.getString("name"));
		}
	}

JiraRestClient


	public static void main(String[] args) throws Exception {
		System.out.println("Step 1: Setup Authentication Information");
		String HOST = jlc.getHost(); // jira.corp.com
		String URL = jlc.getUrl(); // https://jira.corp.com
		String USERNAME = jlc.getUsername(); // ...
		String PASSWORD = jlc.getPassword(); // ...

		System.out.println("Step 2: Install SSL Certificate");		
		SSLHandler.setupSSLContextByInstallingCertification(HOST);

		System.out.println("Step 3: Query Server");		
		final JiraRestClientFactory jiraRestClientFactory = new AsynchronousJiraRestClientFactory();
		JiraRestClient jiraRestClient = jiraRestClientFactory
						.createWithBasicHttpAuthentication(URI.create(URL), USERNAME, PASSWORD);
		
		Iterable basicProjects = jiraRestClient.getProjectClient().getAllProjects().get();
		jiraRestClient.close();

		System.out.println("Step 4: List Projects");
		for(BasicProject project: basicProjects) {
			System.out.println("BasicProject Project Name:"+project.getName());
		}	
	}

WebResource


	public static void main(String[] args) throws Exception {
		System.out.println("Step 1: Setup Authentication Information");
		String HOST = jlc.getHost(); // jira.corp.com
		String URL = jlc.getUrl(); // https://jira.corp.com
		String USERNAME = jlc.getUsername(); // ...
		String PASSWORD = jlc.getPassword(); // ...

		System.out.println("Step 2: Install SSL Certificate");		
		SSLHandler.setupSSLContextByInstallingCertification(HOST);

		System.out.println("Step 3: Query Server");	
		Client client = Client.create();
		client.addFilter(new HTTPBasicAuthFilter(USERNAME, PASSWORD));
		WebResource webResource = client.resource(URL+"/rest/api/2/project");
		ClientResponse response = webResource.type("application/json").accept("application/json").get(ClientResponse.class);
		String jsonResponse = response.getEntity(String.class);
		response.close();
		client.destroy();

		System.out.println("Step 4: List Projects:"+jsonResponse);
		JSONArray projectArray = new JSONArray(jsonResponse);
		for (int i = 0; i < projectArray.length(); i++) {
			JSONObject proj = projectArray.getJSONObject(i);
			System.out.println("JSONObject Project Name:"+proj.getString("name"));
		}
	}

You may need this class for SSL:


public class SSLHandler {
	/**
	 * Installs C:\Program Files\Java\jdk1.7.0_51\jre\lib\security\jssecacerts file
	 * @param host
	 * @throws Exception
	 */
	public static void setupSSLContextByInstallingCertification(String host) {
		System.out.println("Installing SSLContext Certification into Java Home...");
		try {
	        int port = 443;
	        char[] passphrase = "changeit".toCharArray();
			
	        char SEP = File.separatorChar;
	        File file = new File("jssecacerts");
	        if (file.isFile() == false) {
	            File dir = new File(System.getProperty("java.home") + SEP
	                    + "lib" + SEP + "security");
	            file = new File(dir, "jssecacerts");
	            if (file.isFile() == false) {
	                file = new File(dir, "cacerts");
	            }
	        }
	        
	        System.out.println("Loading KeyStore " + file + "...");
	        InputStream in = new FileInputStream(file);
	        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
	        ks.load(in, passphrase);
	        in.close();
	 
	        TrustManagerFactory tmf =
	                TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
	        tmf.init(ks);
	        final X509TrustManager defaultTrustManager = (X509TrustManager) tmf.getTrustManagers()[0];
	        final List chainList = new ArrayList();
	        
			TrustManager tm = new X509TrustManager() {
				public void checkClientTrusted(X509Certificate[] arg0, String arg1)
								throws CertificateException { }
				public void checkServerTrusted(X509Certificate[] arg0, String arg1)
						throws CertificateException {
						chainList.clear();
						if(arg0 != null) {
							for (int i = 0; i < arg0.length; i++) {
								chainList.add(arg0[i]);
							}
						}
						defaultTrustManager.checkServerTrusted(arg0, arg1); }
				public X509Certificate[] getAcceptedIssuers() { return null; }
			};
			
	        SSLContext context = SSLContext.getInstance("TLS");
	        context.init(null, new TrustManager[]{tm}, null);
	        SSLSocketFactory factory = context.getSocketFactory();
	 
	        System.out.println("Opening connection to " + host + ":" + port + "...");
	        SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
	        socket.setSoTimeout(10000);
	        try {
	            socket.startHandshake();
	            System.out.println("No errors, certificate is already trusted");
	        } catch (SSLException e) { }
	        socket.close();
	 
	        if (chainList.isEmpty()) {
	            System.out.println("Could not obtain server certificate chain");
	            return;
	        }
	 
	        System.out.println("Server sent " + chainList.size() + " certificate(s):");

	        MessageDigest sha1 = MessageDigest.getInstance("SHA1");
	        MessageDigest md5 = MessageDigest.getInstance("MD5");
	        for (int i = 0; i > 4]);
            sb.append(HEXDIGITS[b & 15]);
            sb.append(' ');
        }
        return sb.toString();
    }
}

Other Resources for Creating Tickets