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