(no commit message)
[jalview.git] / forester / java / src / org / forester / ws / wabi / RestUtil.java
1 // $Id:
2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
4 //
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.ws.wabi;
27
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import java.io.PrintStream;
32 import java.io.UnsupportedEncodingException;
33 import java.net.URL;
34 import java.net.URLConnection;
35 import java.net.URLEncoder;
36 import java.util.List;
37
38 /**
39  *
40  * This is to access the Web API for Biology (WABI) at DDBJ.
41  * See: http://xml.nig.ac.jp/
42  *
43  */
44 public final class RestUtil {
45
46     final static String         LIST_SEPARATOR = "%0A";
47     final static String         LINE_SEPARATOR = "\n";
48     private final static String BASE_URL       = "http://xml.nig.ac.jp/rest/Invoke";
49     private final static String SERVICE        = "service";
50     private final static String METHOD         = "method";
51     private final static String URL_ENC        = "UTF-8";
52
53     static String encode( final String str ) throws UnsupportedEncodingException {
54         return URLEncoder.encode( str.trim(), URL_ENC );
55     }
56
57     /**
58      * Method for access REST
59      * @param query
60      * service name method name and parameter for executing rest
61      * @return execution result
62      * @throws IOException
63      */
64     public static String getResult( final String query ) throws IOException {
65         final URL url = new URL( BASE_URL );
66         final URLConnection urlc = url.openConnection();
67         urlc.setDoOutput( true );
68         urlc.setAllowUserInteraction( false );
69         final PrintStream ps = new PrintStream( urlc.getOutputStream() );
70         //System.out.println( "query: " + query );
71         ps.print( query.trim() );
72         ps.close();
73         final BufferedReader br = new BufferedReader( new InputStreamReader( urlc.getInputStream() ) );
74         final StringBuffer sb = new StringBuffer();
75         String line = null;
76         while ( ( line = br.readLine() ) != null ) {
77             sb.append( line + LINE_SEPARATOR );
78         }
79         br.close();
80         return sb.toString().trim();
81     }
82
83     public static String getResult( final String service_name, final String method_name, final String parameters )
84             throws IOException {
85         final String service = SERVICE + '=' + encode( service_name );
86         final String method = METHOD + '=' + encode( method_name );
87         return getResult( service + '&' + method + '&' + parameters.trim() );
88     }
89
90     static String listAsString( final List<String> l ) throws UnsupportedEncodingException {
91         final StringBuffer sb = new StringBuffer();
92         for( final String s : l ) {
93             if ( sb.length() > 0 ) {
94                 sb.append( LIST_SEPARATOR );
95             }
96             sb.append( encode( s ) );
97         }
98         return sb.toString();
99     }
100 }