2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.ws.wabi;
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;
34 import java.net.URLConnection;
35 import java.net.URLEncoder;
36 import java.util.List;
40 * This is to access the Web API for Biology (WABI) at DDBJ.
41 * See: http://xml.nig.ac.jp/
44 public final class RestUtil {
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";
53 static String encode( final String str ) throws UnsupportedEncodingException {
54 return URLEncoder.encode( str.trim(), URL_ENC );
58 * Method for access REST
60 * service name method name and parameter for executing rest
61 * @return execution result
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() );
73 final BufferedReader br = new BufferedReader( new InputStreamReader( urlc.getInputStream() ) );
74 final StringBuffer sb = new StringBuffer();
76 while ( ( line = br.readLine() ) != null ) {
77 sb.append( line + LINE_SEPARATOR );
80 return sb.toString().trim();
83 public static String getResult( final String service_name, final String method_name, final String parameters )
85 final String service = SERVICE + '=' + encode( service_name );
86 final String method = METHOD + '=' + encode( method_name );
87 return getResult( service + '&' + method + '&' + parameters.trim() );
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 );
96 sb.append( encode( s ) );