Ignore:
Timestamp:
2008-12-16T14:18:45+13:00 (15 years ago)
Author:
cc108
Message:

add method to deal with Proxy Authentication

File:
1 edited

Legend:

Unmodified
Added
Removed
  • other-projects/trunk/greenstone3-extension/mat/src/org/greenstone/gsdl3_extension/mat/servlet/MatServlet.java

    r18167 r18206  
    1515import org.greenstone.gsdl3.util.GlobalProperties;
    1616import org.greenstone.gsdl3.util.GSPath;
     17import java.net.Authenticator;
     18import java.net.PasswordAuthentication;
    1719
    1820public class MatServlet extends HttpServlet {
     
    3840    private String proxyPortContent = "";
    3941    private String collection_creator = "";
    40    
     42    private String proxyUserName= "";
     43    private String proxyUserPassword = "";
    4144   
    4245    public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
     
    114117    else if (req.getParameter("collName") != null){
    115118        out.println(headerString);
    116         //out.write("<script type=\"text/javascript\" src=\"http://"+hostName+":"+port_number+"/greenstone3/mat/script/getInfomation.js\"></script>\r\n");
    117         //out.write("<script type=\"text/javascript\" src=\"http://yui.yahooapis.com/2.4.1/build/yahoo/yahoo-min.js\"></script>\r\n");
    118         //out.write("<script type=\"text/javascript\" src=\"http://yui.yahooapis.com/2.4.1/build/event/event-min.js\"></script>\r\n");
    119         //out.write("<script type=\"text/javascript\" src=\"http://yui.yahooapis.com/2.4.1/build/connection/connection-min.js\"></script>\r\n");
    120119        out.println("<body>");
    121120        out.println(h1String); 
     
    192191        // trim the URL back and then add on verb=Identify
    193192       
    194        
    195         //System.setProperty("http.proxyHost", "wwwcache.cs.waikato.ac.nz");
    196         //System.setProperty("http.proxyPort","80");
    197         //System.setProperty("http.nonProxyHosts", "localhost|*.waikato.ac.nz");
    198        
     193               
    199194        Properties systemSettings = System.getProperties();
    200195       
     
    215210       
    216211        URL identifyURL = new URL (url.toString() + identifyVerb);
     212   
     213        if( proxyUserName.equals("") || proxyUserPassword.equals("")){}
     214        else{
     215         Authenticator.setDefault( new ProxyAuthenticator(proxyUserName, proxyUserPassword) );
     216        }
     217
    217218        HttpURLConnection connection = (HttpURLConnection)identifyURL.openConnection();
    218         //URLConnection connection = identifyURL.openConnection();         
    219219        connection.connect();
    220        
    221         //BufferedReader inIdentify = new BufferedReader(new InputStreamReader(
    222         //connection.getInputStream()));
     220
    223221           
    224222        Document identifyDocument;
     
    396394        // maybe check the baseURL against the query we issued?
    397395       
    398        
    399         // if we get here we have a valid non-waikato non-local OAI server
    400         // /* process stuff here */
    401         // test with University home page - as it doesn't require
    402         // dealing with the proxy server
    403         // URL url = new URL("http://waikato.ac.nz");
    404         // URL url = new URL(oaiURL);
    405         // need to go through the proxy here
    406         // http://dn.codegear.com/article/29783
    407         // http://java.ittoolbox.com/groups/technical-functional/java-l/response-to-proxy-authentication-exception-71438
    408         // http://www.jguru.com/faq/view.jsp?EID=13186
    409         // http://www.developer.com/java/other/article.php/1551421
    410         // System.setProperty("http.proxyHost","http://proxy.scms.waikato.ac.nz");
    411         // System.setProperty("http.proxyPort","80");
    412         // System.setProperty("http.nonProxyHosts", "localhost|*.waikato.ac.nz");
    413         // build a GS 3 collection from the OAI URL
    414         // http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
    415         // http://codon.kribb.re.kr/wiki/display/Java/Using+Runtime.exec+to+invoke+child+process
    416        
    417396        out.print("</body></html>");
    418397        out.close();   
     
    541520    String cacheDir = gsdl3Root +fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"tmp"+fileSeparator + collName;
    542521
    543     // downloading
    544     //String gs3Root = "/research/cc108/greenstone3Project";
     522
    545523    String gs3Root = gsdl3Root;
    546524    String collectDir = gsdl3Home + fileSeparator +"sites"+fileSeparator+"localsite"+ fileSeparator +"collect";
    547525    String logFile = gsdl3Root +fileSeparator+"ext"+fileSeparator+"mat"+fileSeparator+"tmp"+fileSeparator + "log.txt";
    548     //source /research/cc108/greenstone3Project/gs2build/bin/script/mat-colbuild-download.bash
    549     //source /research/cc108/greenstone3Project/ext/mat/bin/script/mat-colbuild-download.bash
    550    
     526    String proxy_settings = "";
    551527    String matShell  = "";
    552528   
     529   
     530    if(!proxyHostContent.equals("") && !proxyPortContent.equals("")){
     531        proxy_settings = "-proxy_on -proxy_host "+proxyHostContent+" -proxy_port "+proxyPortContent;
     532    }
     533
     534    if(!proxyUserName.equals("") && !proxyUserPassword.equals("")){
     535        proxy_settings = proxy_settings + " -user_name " +proxyUserName+" -user_password "+proxyUserPassword;
     536    }
     537   
     538
    553539    if(System.getProperty("os.name").toLowerCase().indexOf("windows")!=-1){
    554540       
     
    568554        alist.add(logFile);
    569555        alist.add(collection_creator);
    570        
     556        alist.add(proxy_settings);
    571557        String[] arrays = new String[alist.size()];
    572558        for(int i = 0; i< arrays.length; i++){
     
    600586        + logFile
    601587        + " "
    602         + collection_creator;
     588        + collection_creator
     589        + " "
     590        + proxy_settings;
    603591   
    604592    out.println("<p><pre>" + matShell + "</pre></p>");
     
    613601    }
    614602    out.flush();
    615     //p = null;
    616603    p.destroy();
    617604   
     
    880867        proxyPortContent = prop.getProperty("Servlet.proxyPort");
    881868       
     869        proxyUserName = prop.getProperty("Servlet.proxyUserName");
     870        proxyUserPassword = prop.getProperty("Servlet.proxyUserPassword");
     871
    882872        collection_creator = prop.getProperty("Servlet.Collection.Creator");
    883873       
     
    896886    }
    897887}
     888
     889class ProxyAuthenticator extends Authenticator
     890   {
     891
     892    private String username;
     893    private char[] password;
     894
     895   
     896    public ProxyAuthenticator (String username, String password){
     897        this.username = username;
     898        this.password = password.toCharArray();
     899    }
     900   
     901
     902     protected PasswordAuthentication getPasswordAuthentication(){
     903          return new PasswordAuthentication (username, password );
     904        }
     905   }
Note: See TracChangeset for help on using the changeset viewer.