in progress
[jalview.git] / forester / java / src / org / forester / ws / hmmer / Test.java
1
2 package org.forester.ws.hmmer;
3
4 import java.io.BufferedReader;
5 import java.io.DataOutputStream;
6 import java.io.InputStreamReader;
7 import java.net.HttpURLConnection;
8 import java.net.URL;
9 import java.net.URLEncoder;
10
11 public class Test {
12
13     public static void main( final String[] args ) {
14         try {
15             final URL url = new URL( "http://hmmer.janelia.org/search/hmmscan" );
16             final HttpURLConnection connection = ( HttpURLConnection ) url.openConnection();
17             connection.setDoOutput( true );
18             connection.setDoInput( true );
19             connection.setInstanceFollowRedirects( false );
20             connection.setRequestMethod( "POST" );
21             connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" );
22             connection.setRequestProperty( "Accept", "application/json" );
23             //Add the database and the sequence. Add more options as you wish!
24             final String urlParameters = "hmmdb=" + URLEncoder.encode( "pfam", "UTF-8" ) + "&seq="
25                     + ">seq\nEMGPSENDPNLFVALYDFVASGDNTLSITKGEKLRVLGYNHNGEWCEAQTKNGQGWVPSNYITPV"
26                     + "NSLEKHSWYHGPVSRNAAEYLLSSGINGSFLVRESESSPGQRSISLRYEG"
27                     + "RVYHYRINTASDGKLYVSSESRFNTLAELVHHHSTVADGLITTLHYPAP";
28             connection.setRequestProperty( "Content-Length", "" + Integer.toString( urlParameters.getBytes().length ) );
29             //Send request
30             final DataOutputStream wr = new DataOutputStream( connection.getOutputStream() );
31             wr.writeBytes( urlParameters );
32             wr.flush();
33             wr.close();
34             //Now get the redirect URL
35             final URL respUrl = new URL( connection.getHeaderField( "Location" ) );
36             final HttpURLConnection connection2 = ( HttpURLConnection ) respUrl.openConnection();
37             connection2.setRequestMethod( "GET" );
38             connection2.setRequestProperty( "Accept", "text/x-yaml" );
39             //Get the response and print it to the screen
40             final BufferedReader in = new BufferedReader( new InputStreamReader( connection2.getInputStream() ) );
41             String inputLine;
42             while ( ( inputLine = in.readLine() ) != null ) {
43                 System.out.println( inputLine );
44             }
45             in.close();
46         }
47         catch ( final Exception e ) {
48             throw new RuntimeException( e );
49         }
50     }
51 }