Changeset 4507


Ignore:
Timestamp:
Aug 15, 2013, 1:43:07 PM (10 years ago)
Author:
Fredrik Levander
Message:

Fixes #813. Updated mascot results retrieval to use new version of http client.
Changed results retrieval to avoid retrieving results twice.
Updated apache http client jars.
Updated mascot xml results schema
Updated mascot search plugin so that import is using function in results retrieval plug-in.
Removed StreamGobbler? code since it was not used.

Location:
trunk
Files:
3 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/plugin/conf/xsd/mascot_search_results_2.xsd

    r4262 r4507  
    22<!--
    33  @(#)$Source: /vol/cvsroot/mascot/xml/schema/mascot_search_results_2.xsd,v $
    4   @(#)$Revision: 1.15 $
     4  @(#)$Revision: 1.18 $
    55  @(#)$Name:  $
    66-->
     
    435435                </xs:annotation>
    436436              </xs:element>
     437                            <xs:element name="preferred_taxonomy" type="xs:string" minOccurs="0">
     438                                <xs:annotation>
     439                                    <xs:documentation>Taxonomy to prefer when two or more proteins match the same set of peptides or when protein entry in database represents multiple sequences</xs:documentation>
     440                                </xs:annotation>
     441                            </xs:element>
    437442            </xs:sequence>
    438443          </xs:complexType>
     
    923928        <xs:annotation>
    924929          <xs:documentation>Variable modifications encoded as string</xs:documentation>
     930        </xs:annotation>
     931      </xs:element>
     932      <xs:element name="pep_summed_mod_pos" type="xs:string" minOccurs="0">
     933        <xs:annotation>
     934          <xs:documentation>Summed modifications encoded as string</xs:documentation>
     935        </xs:annotation>
     936      </xs:element>
     937      <xs:element name="pep_var_mod_conf" type="xs:string" minOccurs="0">
     938        <xs:annotation>
     939          <xs:documentation>Variable modification site analysis confidence (percentage between 0% and 100%)</xs:documentation>
    925940        </xs:annotation>
    926941      </xs:element>
  • trunk/plugin/src/org/proteios/plugins/MascotResultRetrievalPlugin.java

    r3860 r4507  
    2828package org.proteios.plugins;
    2929
    30 import org.apache.commons.httpclient.HttpClient;
    31 import org.apache.commons.httpclient.HttpStatus;
    32 import org.apache.commons.httpclient.methods.GetMethod;
    33 import org.apache.commons.httpclient.methods.PostMethod;
    34 import org.apache.commons.httpclient.methods.multipart.FilePart;
    35 import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
    36 import org.apache.commons.httpclient.methods.multipart.Part;
    37 import org.apache.commons.httpclient.methods.multipart.PartSource;
    38 import org.apache.commons.httpclient.methods.multipart.StringPart;
     30import org.apache.http.HttpResponse;
     31import org.apache.http.HttpStatus;
     32import org.apache.http.client.methods.HttpGet;
     33import org.apache.http.client.methods.HttpPost;
     34import org.apache.http.entity.mime.MultipartEntity;
     35import org.apache.http.entity.mime.content.StringBody;
     36import org.apache.http.impl.client.BasicResponseHandler;
     37import org.apache.http.impl.client.DefaultHttpClient;
     38import org.apache.http.impl.client.LaxRedirectStrategy;
    3939import org.proteios.core.BaseException;
    4040import org.proteios.core.DbControl;
     
    5252import org.proteios.core.plugin.Response;
    5353
    54 import java.io.BufferedReader;
    55 import java.io.FileInputStream;
    56 import java.io.FileOutputStream;
    57 import java.io.IOException;
     54import java.io.ByteArrayInputStream;
    5855import java.io.InputStream;
    59 import java.io.InputStreamReader;
    60 import java.io.OutputStream;
    61 import java.io.PrintWriter;
     56import java.io.UnsupportedEncodingException;
    6257import java.util.ArrayList;
    63 import java.util.Calendar;
    64 import java.util.GregorianCalendar;
    6558import java.util.Hashtable;
    66 import java.util.List;
    6759import java.util.regex.Matcher;
    6860import java.util.regex.Pattern;
     
    118110      .getValue("mascotExportScriptPath");
    119111    // Get the Mascot result archive directory path
    120     String mascotArchivePath = (String) job
    121       .getValue("mascotArchivePath");
     112    String mascotArchivePath = (String) job.getValue("mascotArchivePath");
    122113    // Get the Mascot result date
    123114    String mascotResultDateInput = (String) job
     
    139130    // Get id for the directory to upload the file to
    140131    Integer dirId = (Integer) job.getValue("dirId");
    141     // 
     132    //
    142133    int mascotSearchNumber = 0;
    143134    if (mascotSearchNumberInput != null)
     
    166157      mascotQueryPeaksStr = new String("1");
    167158    }
    168     // Mascot "query_master" flag must be set to "1" for any other "query" setting to work
     159    // Mascot "query_master" flag must be set to "1" for any other "query"
     160    // setting to work
    169161    String mascotQueryMasterStr = new String("0");
    170162    if (mascotQueryPeaksInput != null && mascotQueryPeaksInput)
     
    172164      mascotQueryMasterStr = new String("1");
    173165    }
    174     // 
     166    //
    175167    log.debug("mascotServerURLStr = \"" + mascotServerURLStr + "\"");
    176168    log.debug("mascotExportScriptPath = \"" + mascotExportScriptPath + "\"");
     
    189181    log.debug("dirId = " + dirId);
    190182    // Hash table with extra arguments
    191     Hashtable<String, String> extraArgHashtable = new Hashtable<String, String>(0);
     183    Hashtable<String, String> extraArgHashtable = new Hashtable<String, String>(
     184      0);
    192185    extraArgHashtable.put("show_unassigned", mascotShowUnassignedStr);
    193186    extraArgHashtable.put("query_master", mascotQueryMasterStr);
    194187    extraArgHashtable.put("query_peaks", mascotQueryPeaksStr);
    195188    // Perform Mascot result retrieval using web interface
    196     mascotResultExportViaWebInterface(request, response, progress,
    197       mascotServerURLStr, mascotExportScriptPath,
    198       mascotArchivePath, mascotResultDate,
    199       mascotResultFilenamePrefix, mascotSearchNumber,
     189    DbControl dc = sc.newDbControl();
     190    mascotResultExportViaWebInterface(dc, request, response, progress,
     191      mascotServerURLStr, mascotExportScriptPath, mascotArchivePath,
     192      mascotResultDate, mascotResultFilenamePrefix, mascotSearchNumber,
    200193      extraArgHashtable, dirId);
    201194    done();
     
    206199  {
    207200    About about = new AboutImpl("MascotResultRetrievalPlugin",
    208       "Retrieves Mascot result files",
    209       "1.0", "", "Olle Månsson", "olle@thep.lu.se", "www.proteios.org");
     201      "Retrieves Mascot result files", "1.1", "", "Olle Månsson",
     202      "olle@thep.lu.se", "www.proteios.org");
    210203    return about;
    211204  }
     
    219212
    220213  /**
    221    * Start a job that performs an X!Tandem search using a web interface to an
    222    * X!Tandem search engine.
     214   * The function will try to retrieve a Mascot results in XML format from the
     215   * indicated server. Using the DbControl it will be save as a file in the
     216   * indicated directory. Note that the DbControl will be closed.
    223217   *
     218   * @param dc DbControl to used for file creation
    224219   * @param request Request
    225220   * @param response Response
     
    231226   * @param mascotResultFilenamePrefix String Mascot result filename prefix
    232227   * @param mascotSearchNumber int Mascot search number
    233    * @param extraArgHashtable Hashtable<String, String> Key-value pairs for extra arguments.
     228   * @param extraArgHashtable Hashtable<String, String> Key-value pairs for
     229   *        extra arguments.
    234230   * @param dirId Integer Id for the directory to upload the file to
    235231   */
    236   private void mascotResultExportViaWebInterface(Request request,
    237       Response response, ProgressReporter progress,
     232  protected static File mascotResultExportViaWebInterface(DbControl dc,
     233      Request request, Response response, ProgressReporter progress,
    238234      String mascotServerURLStr, String mascotExportScriptPath,
    239235      String mascotArchivePath, String mascotResultDate,
     
    248244    log.debug("mascotSearchNumber = " + mascotSearchNumber);
    249245    // Extra arguments
    250     for (String argName: extraArgHashtable.keySet())
     246    for (String argName : extraArgHashtable.keySet())
    251247    {
    252248      String argVal = extraArgHashtable.get(argName);
     
    264260    }
    265261    //
    266     log
    267       .debug("mascotExportScriptURLStr = \"" + mascotExportScriptURLStr + "\"");
    268     log
    269       .debug("mascotArchiveURLStr = \"" + mascotArchiveURLStr + "\"");
     262    log.debug("mascotExportScriptURLStr = \"" + mascotExportScriptURLStr + "\"");
     263    log.debug("mascotArchiveURLStr = \"" + mascotArchiveURLStr + "\"");
    270264    //
    271265    // "../data/20080924/F009765.dat");
    272     String mascotSearchNumberStr = leftPaddedString(Integer.toString(mascotSearchNumber), 6, '0');
     266    String mascotSearchNumberStr = leftPaddedString(
     267      Integer.toString(mascotSearchNumber), 6, '0');
    273268    log.debug("mascotSearchNumberStr = \"" + mascotSearchNumberStr + "\"");
    274269    String resultFilePath = ".." + mascotArchivePath + "/" + mascotResultDate + "/" + mascotResultFilenamePrefix + mascotSearchNumberStr + ".dat";
    275270    log.debug("resultFilePath = \"" + resultFilePath + "\"");
    276271    //
    277     DbControl dc = sc.newDbControl();
    278     ItemFactory factory = new ItemFactory(dc);
    279272    // Hash table with default settings
    280273    Hashtable<String, String> mascotResultExportArgHashtable = fetchMascotResultExportDefaultSettings();
    281274    // Apache HttpClient stuff
    282     PostMethod postMethod = null;
    283     HttpClient httpClient = null;
    284     List<Part> partsList = new ArrayList<Part>(0);
    285     String name = null;
    286     String value = null;
     275    HttpPost postMethod = null;
    287276    // Prepare HTTP post
     277    postMethod = new HttpPost(mascotExportScriptURLStr);
     278    MultipartEntity reqEntity = new MultipartEntity();
    288279    try
    289280    {
    290       // Mascot result file to download
    291       name = new String("file");
    292       //value = new String("../data/20080924/F009765.dat");
    293       value = resultFilePath;
    294       log.debug("name = \"" + name + "\", value = \"" + value + "\"");
    295       partsList.add(new StringPart(name, value));
    296       log
    297         .debug("partsList.get(" + (partsList.size() - 1) + ") = " + partsList
    298           .get(partsList.size() - 1));
    299       // Default settings
    300       for (String argName: mascotResultExportArgHashtable.keySet())
    301       {
    302         name = argName;
    303         value = mascotResultExportArgHashtable.get(argName);
    304         log.debug("name = \"" + argName + "\", value = \"" + value + "\"");
    305         partsList.add(new StringPart(name, value));
    306         log
    307           .debug("partsList.get(" + (partsList.size() - 1) + ") = " + partsList
    308             .get(partsList.size() - 1));
     281      reqEntity.addPart("file", new StringBody(resultFilePath));
     282      for (String argName : mascotResultExportArgHashtable.keySet())
     283      {
     284        reqEntity.addPart(argName, new StringBody(
     285          mascotResultExportArgHashtable.get(argName)));
    309286      }
    310287      // Extra arguments
    311       for (String argName: extraArgHashtable.keySet())
    312       {
    313         name = argName;
    314         value = extraArgHashtable.get(argName);
    315         log.debug("name = \"" + argName + "\", value = \"" + value + "\"");
    316         partsList.add(new StringPart(name, value));
    317         log
    318           .debug("partsList.get(" + (partsList.size() - 1) + ") = " + partsList
    319             .get(partsList.size() - 1));
    320       }
    321       //
    322       // Parts list is converted to an array one element
    323       // at a time, to avoid casting problems with
    324       // (Part[]) partsList.toArray().
    325       //
    326       Part[] parts = new Part[partsList.size()];
    327       for (int i = 0; i < partsList.size(); i++)
    328       {
    329         parts[i] = (Part) partsList.get(i);
    330       }
    331       log.debug("parts.length = " + parts.length);
    332       postMethod = new PostMethod(mascotExportScriptURLStr);
    333       postMethod.setRequestEntity(new MultipartRequestEntity(parts,
    334         postMethod.getParams()));
    335     }
    336     catch (Exception e)
    337     {
    338       log.debug("Exception when trying to get HttpClient items : " + e);
    339     }
     288      for (String argName : extraArgHashtable.keySet())
     289      {
     290        reqEntity.addPart(argName,
     291          new StringBody(extraArgHashtable.get(argName)));
     292      }
     293    }
     294    catch (UnsupportedEncodingException e1)
     295    {
     296      // TODO Auto-generated catch block
     297      throw new BaseException(
     298        "Error encoding Mascot server request: " + e1);
     299    }
     300
    340301    // Execute request
    341     String content = null;
     302    postMethod.setEntity(reqEntity);
    342303    String resultFilename = null;
    343304    String usedResultFilename = null;
    344     String inputDataFilename = new String("unknown");
    345     boolean imported = false;
    346     String ioErrorString = null;
    347305    log.debug("resultFilePath = \"" + resultFilePath + "\"");
    348     String inputFilename = fetchMascotInputFilename(postMethod);
    349     log.debug("inputFilename = \"" + inputFilename + "\"");
    350     // If input filename not found, check for errors
    351306    String errorText = null;
    352     if (inputFilename == null || inputFilename.equals(""))
    353     {
    354       errorText = checkMascotOutputForError(postMethod);
    355     }
    356307    log.debug("errorText = \"" + errorText + "\"");
    357308    resultFilename = mascotResultFilenamePrefix + mascotSearchNumberStr + ".dat";
    358309    usedResultFilename = mascotResultDate + "_" + mascotResultFilenamePrefix + mascotSearchNumberStr + ".dat";
    359     String filenameOfUploadedFile = constructFilenameOfUploadedFile(inputFilename, usedResultFilename);
    360     FileType fileType = factory.getBySystemId(FileType.class, FileType.MASCOT_XML);
    361     String description = new String("Mascot result - date: " + mascotResultDate + " file: " + resultFilename);
     310    String description = new String(
     311      "Mascot result - date: " + mascotResultDate + " file: " + resultFilename);
    362312    log.debug("resultFilename = \"" + resultFilename + "\"");
    363313    log.debug("usedResultFilename = \"" + usedResultFilename + "\"");
    364     log.debug("filenameOfUploadedFile = \"" + filenameOfUploadedFile + "\"");
    365     log.debug("fileType = \"" + fileType + "\"");
    366314    log.debug("description = \"" + description + "\"");
    367     String doneMessage = new String("");
    368     String doneMessagePart2 = new String("");
     315    File returnedFile = null;
    369316    // Only upload file if no error indicated
    370     if (errorText == null || errorText.equals(""))
    371     {
    372       try
    373       {
    374         uploadFileViaWebInterface(postMethod,
    375           filenameOfUploadedFile, dirId,
    376           description, fileType);
    377         doneMessagePart2 = doneMessagePart2 + "\n" + "Result file uploaded: " + filenameOfUploadedFile;
    378         imported = true;
    379       }
    380       catch (BaseException e)
    381       {
    382         doneMessagePart2 = doneMessagePart2 + "\n" + "Result file upload error: " + e;
    383       }
    384     }
    385     else
    386     {
    387       doneMessagePart2 = doneMessagePart2 + "\n" + "Result file upload error: " + errorText;
    388     }
    389     //
    390     if (imported)
    391     {
    392       doneMessage = new String("Mascot result retrieval finished");
     317    try
     318    {
     319      returnedFile = uploadFileViaWebInterface(postMethod, dc,
     320        usedResultFilename, dirId, description);
     321      String doneMessage = new String("Mascot result retrieval finished");
    393322      doneMessage = doneMessage + "\n" + "Mascot result file: " + resultFilename;
    394       doneMessage = doneMessage + doneMessagePart2;
    395323      response.setDone(doneMessage);
    396324    }
    397     else
    398     {
     325    catch (BaseException e)
     326    {
     327      String errorMessage = new String(
     328        "Nothing retrieved\nResult file upload error: " + e);
    399329      ArrayList<Throwable> err = new ArrayList<Throwable>(1);
    400       // Construct job error message
    401       String errorMessage = new String("Nothing retrieved");
    402       // Add Mascot data input filename
    403       errorMessage = errorMessage + "\n" + "Mascot result file path: " + resultFilePath;
    404       //
    405       if (ioErrorString != null && !ioErrorString.equals(""))
    406       {
    407         errorMessage = errorMessage + "\n" + ioErrorString;
    408       }
    409       errorMessage = errorMessage + "\n" + doneMessagePart2;
    410       //
    411       err.add(new BaseException("No Mascot result retrieval could be performed"));
    412330      response.setError(errorMessage, err);
     331      err.add(new BaseException(
     332        "No Mascot result retrieval could be performed"));
    413333    }
    414334    log.debug("End of method.");
     335    return returnedFile;
    415336  }
    416337
    417338
    418339  /**
    419    * Fetches Mascot result export default settings
    420    * in the form of a hash table with name-value pairs.
     340   * Fetches Mascot result export default settings in the form of a hash table
     341   * with name-value pairs.
    421342   *
    422    * @return Hashtable<String, String> Name-value pair hash table of Mascot result export default settings.
     343   * @return Hashtable<String, String> Name-value pair hash table of Mascot
     344   *         result export default settings.
    423345   */
    424   private Hashtable<String, String> fetchMascotResultExportDefaultSettings()
     346  private static Hashtable<String, String> fetchMascotResultExportDefaultSettings()
    425347  {
    426348    // Hash table with default settings
    427     Hashtable<String, String> mascotResultExportArgHashtable = new Hashtable<String, String>(0);
     349    Hashtable<String, String> mascotResultExportArgHashtable = new Hashtable<String, String>(
     350      0);
    428351    mascotResultExportArgHashtable.put("export_format", "XML");
    429352    mascotResultExportArgHashtable.put("do_export", "1");
     353    mascotResultExportArgHashtable.put("generate_file", "1");
    430354    // report : Number of hits to be reported (0 = AUTO)
    431355    mascotResultExportArgHashtable.put("report", "0");
     
    440364    // show_unassigned : Include peptides not assigned to proteins
    441365    // show_inassigned settings excluded here as it is user-selected
    442     //mascotResultExportArgHashtable.put("show_unassigned", "0");
     366    // mascotResultExportArgHashtable.put("show_unassigned", "0");
    443367    mascotResultExportArgHashtable.put("show_same_sets", "1");
    444368    mascotResultExportArgHashtable.put("_showsubsets", "1");
     
    484408
    485409  /**
    486    * Construct filename for uploaded search result file.
    487    * The constructed filename consists of the (possibly modified)
    488    * base name of the input file, an underscore, plus the modified
    489    * result filename consisting of the date, an underscore, and the
    490    * result filename with file extension ".dat" exchanged for ".xml".
     410   * Construct filename for uploaded search result file. The constructed
     411   * filename consists of the (possibly modified) base name of the input file,
     412   * an underscore, plus the modified result filename consisting of the date,
     413   * an underscore, and the result filename with file extension ".dat"
     414   * exchanged for ".xml".
    491415   *
    492416   * @param inputFilename String Filename of input file for search.
     
    494418   * @return String The constructed filename for the uploaded result file.
    495419   */
    496   private String constructFilenameOfUploadedFile(String inputFilename,
     420  private static String constructFilenameOfUploadedFile(String inputFilename,
    497421      String filename)
    498422  {
     
    535459      }
    536460      log.debug("tmpStr after removing file extension = \"" + tmpStr + "\"");
    537       // Replace any remaining blanks ' ' in input filename with underscores '_'
     461      // Replace any remaining blanks ' ' in input filename with
     462      // underscores '_'
    538463      tmpStr = tmpStr.replaceAll(" ", "_");
    539464      log.debug("tmpStr after replacing blanks ' ' with underscores '_' = \"" + tmpStr + "\"");
    540       // Replace any remaining dots '.' in input filename with underscores '_'
     465      // Replace any remaining dots '.' in input filename with underscores
     466      // '_'
    541467      tmpStr = tmpStr.replaceAll("\\.", "_");
    542468      log.debug("tmpStr after replacing dots '.' with underscores '_' = \"" + tmpStr + "\"");
     
    576502   *         'minChars' characters.
    577503   */
    578   private String leftPaddedString(String inStr, int minChars, char padChar)
     504  private static String leftPaddedString(String inStr, int minChars,
     505      char padChar)
    579506  {
    580507    String startStr = null;
     
    606533
    607534  /**
    608    * Fetches Mascot input filename from parsed result file
    609    * via web interface from HTTPClient PostMethod.
     535   * Uploads file via web interface into Proteios from HTTPClient HttpPost.
    610536   *
    611    * @param postMethod PostMethod HTTPClient PostMethod for getting input stream of Mascot result file.
    612    * @return String Mascot input filename for result file, or null if not found.
    613    */
    614   private String fetchMascotInputFilename(PostMethod postMethod)
    615   {
    616     String inputFilename = null;
    617     try
    618     {
    619       // Get HTTP client
    620       HttpClient httpClient = new HttpClient();
    621       int statusCode = httpClient.executeMethod(postMethod);
    622       // Check status code
    623       log.debug("Response status code: " + statusCode);
    624       if (statusCode != HttpStatus.SC_OK)
    625       {
    626         log.warn("Error when trying to execute PostMethod: " + postMethod
    627           .getStatusLine());
    628       }
    629       // Get response
    630       //InputStream inStream = postMethod.getResponseBodyAsStream();
    631       String content = postMethod.getResponseBodyAsString();
    632       String regexStr = new String("<FILENAME>.+</FILENAME>");
    633       log.debug("regex string = \"" + regexStr + "\"");
    634       Pattern p = Pattern.compile(regexStr);
    635       Matcher m = p.matcher(content);
    636       if (m.find())
    637       {
    638         // Extract filename from XML tag
    639         String hitString = m.group(0);
    640         log.debug("hitString = \"" + hitString + "\"");
    641         hitString = hitString.replaceAll("<FILENAME>", "");
    642         log.debug("hitString = \"" + hitString + "\"");
    643         hitString = hitString.replaceAll("</FILENAME>", "");
    644         log.debug("hitString = \"" + hitString + "\"");
    645         if (hitString != null)
    646         {
    647           inputFilename = hitString;
    648         }
    649       }
    650       else
    651       {
    652         log.debug("regex string \"" + regexStr + "\" not found in file contents.");
    653       }
    654     }
    655     catch (Exception e)
    656     {
    657       log.error(e.getMessage(), e);
    658     }
    659     finally
    660     {
    661       // Release current connection to the connection pool once you are
    662       // done
    663       if (postMethod != null)
    664       {
    665         log.debug("Trying to release connection to PostMethod");
    666         postMethod.releaseConnection();
    667         log.debug("After trying to release connection to PostMethod");
    668       }
    669       else
    670       {
    671         log.debug("Connection to PostMethod already closed.");
    672       }
    673     }
    674     return inputFilename;
    675   }
    676 
    677 
    678   /**
    679    * Checks Mascot output via web interface from
    680    * HTTPClient PostMethod for indications of problems.
    681    *
    682    * @param postMethod PostMethod HTTPClient PostMethod for getting input stream of Mascot result file.
    683    * @return String Error text to display, or null if no error found.
    684    */
    685   private String checkMascotOutputForError(PostMethod postMethod)
    686   {
    687     String errorText = null;
    688     try
    689     {
    690       // Get HTTP client
    691       HttpClient httpClient = new HttpClient();
    692       int statusCode = httpClient.executeMethod(postMethod);
    693       // Check status code
    694       log.debug("Response status code: " + statusCode);
    695       if (statusCode != HttpStatus.SC_OK)
    696       {
    697         log.warn("Error when trying to execute PostMethod: " + postMethod
    698           .getStatusLine());
    699       }
    700       // Get response
    701       //InputStream inStream = postMethod.getResponseBodyAsStream();
    702       boolean errorIndicated = false;
    703       String content = postMethod.getResponseBodyAsString();
    704       String regexStr = new String("Software error:");
    705       log.debug("regex string = \"" + regexStr + "\"");
    706       Pattern p = Pattern.compile(regexStr);
    707       Matcher m = p.matcher(content);
    708       if (m.find())
    709       {
    710         errorIndicated = true;
    711       }
    712       else
    713       {
    714         log.debug("regex string \"" + regexStr + "\" not found in file contents.");
    715       }
    716       if (errorIndicated)
    717       {
    718         regexStr = new String("<pre>.+</pre>");
    719         log.debug("regex string = \"" + regexStr + "\"");
    720         p = Pattern.compile(regexStr);
    721         m = p.matcher(content);
    722         if (m.find())
    723         {
    724           // Extract error message from HTML tag
    725           String hitString = m.group(0);
    726           log.debug("hitString = \"" + hitString + "\"");
    727           hitString = hitString.replaceAll("<pre>", "");
    728           log.debug("hitString = \"" + hitString + "\"");
    729           hitString = hitString.replaceAll("</pre>", "");
    730           log.debug("hitString = \"" + hitString + "\"");
    731           if (hitString != null)
    732           {
    733             errorText = hitString;
    734           }
    735         }
    736         else
    737         {
    738           log.debug("regex string \"" + regexStr + "\" not found in file contents.");
    739         }
    740         if (errorText == null || errorText.equals(""))
    741         {
    742           errorText = new String("Software error reported by Mascot server, but error text could not be retrieved.");
    743         }
    744       }
    745     }
    746     catch (Exception e)
    747     {
    748       log.error(e.getMessage(), e);
    749     }
    750     finally
    751     {
    752       // Release current connection to the connection pool once you are
    753       // done
    754       if (postMethod != null)
    755       {
    756         log.debug("Trying to release connection to PostMethod");
    757         postMethod.releaseConnection();
    758         log.debug("After trying to release connection to PostMethod");
    759       }
    760       else
    761       {
    762         log.debug("Connection to PostMethod already closed.");
    763       }
    764     }
    765     return errorText;
    766   }
    767 
    768 
    769   /**
    770    * Uploads file via web interface into Proteios
    771    * from HTTPClient PostMethod.
    772    *
    773    * @param postMethod PostMethod HTTPClient PostMethod for getting input stream of file to upload
     537   * @param postMethod PostMethod HTTPClient PostMethod for getting input
     538   *        stream of file to upload
    774539   * @param filenameOfUploadedFile String Filename to use for uploaded file
    775540   * @param dirId Integer Database id of directory to put file item in
     
    778543   * @throws BaseException
    779544   */
    780   private void uploadFileViaWebInterface(PostMethod postMethod,
    781       String filenameOfUploadedFile, Integer dirId,
    782       String descr, FileType fileType)
     545  private static File uploadFileViaWebInterface(HttpPost postMethod,
     546      DbControl dc, String filenameOfUploadedFile, Integer dirId,
     547      String descr)
    783548      throws BaseException
    784549  {
    785     log
    786       .debug("filenameOfUploadedFile = \"" + filenameOfUploadedFile + "\"");
     550    log.debug("filenameOfUploadedFile = \"" + filenameOfUploadedFile + "\"");
    787551    log.debug("dirId = " + dirId);
    788552    log.debug("descr = \"" + descr + "\"");
    789     log.debug("fileType = \"" + fileType + "\"");
    790553    /*
    791554     * If there is an active project the file is saved in the project
    792555     * directory, otherwise we use the users home directory.
    793556     */
    794     DbControl dc = sc.newDbControl();
     557
    795558    ItemFactory factory = new ItemFactory(dc);
     559    FileType fileType = factory.getBySystemId(FileType.class,
     560      FileType.MASCOT_XML);
     561    log.debug("fileType = \"" + fileType + "\"");
     562
    796563    //
    797564    Directory dir = null;
     
    812579    File file = factory.create(File.class);
    813580    file.setDirectory(dir);
    814     file.setName(filenameOfUploadedFile);
     581
    815582    if (descr != null)
    816583    {
    817584      file.setDescription(descr);
    818585    }
    819     file.setMimeType("text/xml");
     586    file.setMimeType("application/xml");
    820587    file.setFileType(fileType);
    821588    dc.saveItem(file);
     589    DefaultHttpClient httpClient = new DefaultHttpClient();
    822590    //
    823591    try
    824592    {
    825593      // Get HTTP client
    826       HttpClient httpClient = new HttpClient();
    827       int statusCode = httpClient.executeMethod(postMethod);
     594
     595      httpClient.setRedirectStrategy(new LaxRedirectStrategy());
     596      HttpResponse response = httpClient.execute(postMethod);
    828597      // Check status code
    829       log.debug("Response status code: " + statusCode);
    830       if (statusCode != HttpStatus.SC_OK)
    831       {
    832         log.warn("Error when trying to execute PostMethod: " + postMethod
     598      log.debug("Response status code: " + response.getStatusLine()
     599        .getStatusCode());
     600      if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
     601      {
     602        log.warn("Error when trying to execute PostMethod: " + response
    833603          .getStatusLine());
    834604      }
    835605      // Get response
    836       InputStream inStream = postMethod.getResponseBodyAsStream();
     606      BasicResponseHandler handler = new BasicResponseHandler();
     607      String content = handler.handleResponse(response);
     608      if (content.contains("continuation-link"))
     609      {
     610        String linkRegExp = "continuation-link\" href=\"(.*)\">Continue";
     611        Pattern p = Pattern.compile(linkRegExp);
     612        Matcher m = p.matcher(content);
     613        if (m.find())
     614        {
     615          log.debug("Redirect:" + m.group(1));
     616          HttpGet request = new HttpGet(m.group(1));
     617          response = httpClient.execute(request);
     618          if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
     619          {
     620            log.warn("Error when trying to execute PostMethod: " + response
     621              .getStatusLine());
     622          }
     623          content = handler.handleResponse(response);
     624        }
     625      }
     626      String regexStr = new String("<FILENAME>(.+)</FILENAME>");
     627      log.debug("regex string = \"" + regexStr + "\"");
     628      Pattern p = Pattern.compile(regexStr);
     629      Matcher m = p.matcher(content);
     630      if (m.find())
     631      {
     632        // Extract filename from XML tag
     633        filenameOfUploadedFile = constructFilenameOfUploadedFile(
     634          m.group(1), filenameOfUploadedFile);
     635        log.debug("file name retrieved:" + m.group(1));
     636      }
     637      else
     638      {
     639        log.debug("regex string \"" + regexStr + "\" not found in file contents.");
     640        log.debug("Content:" + content);
     641        if (content.contains("Software error:"))
     642        {
     643          log.warn("Software error reported");
     644          p = Pattern.compile("<pre>(.+)</pre>");
     645          m = p.matcher(content);
     646          if (m.find())
     647          {
     648            String serverErrorMessage = m.group(1);
     649            throw new BaseException(
     650              "Error reported by server:" + serverErrorMessage);
     651          }
     652        }
     653        else
     654          throw new BaseException(
     655            "Couldn't parse file name in server reponse. Content length:" + content
     656              .length());
     657      }
     658
     659      InputStream inStream = new ByteArrayInputStream(
     660        content.getBytes("UTF-8"));
     661      file.setName(filenameOfUploadedFile);
    837662      file.upload(inStream, false);
    838663      dc.commit();
     
    859684        log.debug("Connection to PostMethod already closed.");
    860685      }
    861     }
     686      httpClient.getConnectionManager().shutdown();
     687    }
     688    return file;
    862689  }
    863690}
  • trunk/plugin/src/org/proteios/plugins/SearchMascotPlugin.java

    r4237 r4507  
    3939import org.proteios.core.BaseException;
    4040import org.proteios.core.DbControl;
    41 import org.proteios.core.Directory;
    4241import org.proteios.core.File;
    4342import org.proteios.core.FileParameterType;
    44 import org.proteios.core.FileType;
    4543import org.proteios.core.ItemFactory;
    4644import org.proteios.core.Job;
     
    5755import org.proteios.io.MascotParameterSet;
    5856
    59 import java.io.BufferedReader;
    6057import java.io.IOException;
    6158import java.io.InputStream;
    62 import java.io.InputStreamReader;
    63 import java.io.OutputStream;
    64 import java.io.PrintWriter;
    6559import java.util.ArrayList;
    6660import java.util.Hashtable;
     
    7670    implements AbortablePluginInterface
    7771{
    78   private class StreamGobbler
    79       extends Thread
    80   {
    81     InputStream is;
    82     String type;
    83     OutputStream os;
    84     String content = null;
    85 
    86 
    87     StreamGobbler(InputStream is, String type)
    88     {
    89       this(is, type, null);
    90     }
    91 
    92 
    93     StreamGobbler(InputStream is, String type, OutputStream redirect)
    94     {
    95       this.is = is;
    96       this.type = type;
    97       this.os = redirect;
    98     }
    99 
    100 
    101     public String getContent()
    102     {
    103       return this.content;
    104     }
    105 
    106 
    107     public void run()
    108     {
    109       PrintWriter pw;
    110       InputStreamReader isr;
    111       BufferedReader br;
    112       String line;
    113 
    114       try
    115       {
    116         pw = null;
    117         if (this.os != null)
    118         {
    119           pw = new PrintWriter(os);
    120         }
    121         isr = new InputStreamReader(is);
    122         br = new BufferedReader(isr);
    123         line = null;
    124         while ((line = br.readLine()) != null)
    125         {
    126           if (pw != null)
    127           {
    128             pw.println(line);
    129           }
    130           System.out
    131             .println(this.getClass().getSimpleName() + ": " + type + ">" + line);
    132           // Append text to content string
    133           if (content == null)
    134           {
    135             content = line;
    136           }
    137           else
    138           {
    139             content = content + "\n" + line;
    140           }
    141         }
    142         if (pw != null)
    143         {
    144           pw.flush();
    145         }
    146       }
    147       catch (IOException ioe)
    148       {
    149         ioe.printStackTrace();
    150       }
    151     }
    152   }
    15372
    15473  private class FileItemPartSource
     
    448367    About about = new AboutImpl("SearchMascotPlugin",
    449368      "Performs Mascot search for spectrum file items in Proteios",
    450       "1.0", "", "Olle Månsson", "olle@thep.lu.se", "www.proteios.org");
     369      "1.1", "", "Olle Månsson", "olle@thep.lu.se", "www.proteios.org");
    451370    return about;
    452371  }
     
    997916        extraArgHashtable.put("query_peaks", mascotQueryPeaksStr);
    998917        // Perform Mascot result retrieval using web interface
    999         File resultFile = mascotResultExportViaWebInterface(request, response, progress,
     918        File resultFile = MascotResultRetrievalPlugin.mascotResultExportViaWebInterface(sc.newDbControl(), request, response, progress,
    1000919          mascotWebServerURLStr, mascotWebExportScriptPath,
    1001920          mascotWebArchivePath, mascotResultDate,
     
    1025944      }
    1026945      response.setDone(doneMessage);
    1027       dc.commit();
    1028946    }
    1029947    else
     
    1051969    }
    1052970    log.debug("End of method.");
    1053   }
    1054 
    1055 
    1056   /**
    1057    * Mascot results XML export
    1058    *
    1059    * @param request Request
    1060    * @param response Response
    1061    * @param progress ProgressReporter
    1062    * @param mascotServerURLStr String URL to the Mascot server
    1063    * @param mascotExportScriptPath String Mascot result export script path
    1064    * @param mascotArchivePath String Mascot result archive directory path
    1065    * @param mascotResultDate String Result date on Mascot server
    1066    * @param mascotResultFilenamePrefix String Mascot result filename prefix
    1067    * @param mascotSearchNumber int Mascot search number
    1068    * @param extraArgHashtable Hashtable<String, String> Key-value pairs for
    1069    *        extra arguments.
    1070    * @param dirId Integer Id for the directory to upload the file to
    1071    * @return File The uploaded file if OK, otherwise null.
    1072    */
    1073   private File mascotResultExportViaWebInterface(Request request,
    1074       Response response, ProgressReporter progress,
    1075       String mascotServerURLStr, String mascotExportScriptPath,
    1076       String mascotArchivePath, String mascotResultDate,
    1077       String mascotResultFilenamePrefix, int mascotSearchNumber,
    1078       Hashtable<String, String> extraArgHashtable, Integer dirId)
    1079   {
    1080     log.debug("mascotServerURLStr = \"" + mascotServerURLStr + "\"");
    1081     log
    1082       .debug("mascotExportScriptPath = \"" + mascotExportScriptPath + "\"");
    1083     log.debug("mascotArchivePath = \"" + mascotArchivePath + "\"");
    1084     log.debug("mascotResultDate = \"" + mascotResultDate + "\"");
    1085     log
    1086       .debug("mascotResultFilenamePrefix = \"" + mascotResultFilenamePrefix + "\"");
    1087     log.debug("mascotSearchNumber = " + mascotSearchNumber);
    1088     // Extra arguments
    1089     for (String argName : extraArgHashtable.keySet())
    1090     {
    1091       String argVal = extraArgHashtable.get(argName);
    1092       log.debug("Extra argument " + argName + "=" + argVal);
    1093     }
    1094     //
    1095     String mascotExportScriptURLStr = mascotServerURLStr + "/" + mascotExportScriptPath;
    1096     String mascotArchiveURLStr = mascotServerURLStr + "/" + mascotArchivePath;
    1097     // Make mascotArchiveURLStr easy to read, since it may be used in
    1098     // messages
    1099     if (mascotServerURLStr.endsWith("/") || mascotArchivePath
    1100       .startsWith("/"))
    1101     {
    1102       mascotArchiveURLStr = mascotServerURLStr + mascotArchivePath;
    1103     }
    1104     //
    1105     log
    1106       .debug("mascotExportScriptURLStr = \"" + mascotExportScriptURLStr + "\"");
    1107     log.debug("mascotArchiveURLStr = \"" + mascotArchiveURLStr + "\"");
    1108     //
    1109     // "../data/20080924/F009765.dat");
    1110     String mascotSearchNumberStr = leftPaddedString(Integer
    1111       .toString(mascotSearchNumber), 6, '0');
    1112     log.debug("mascotSearchNumberStr = \"" + mascotSearchNumberStr + "\"");
    1113     String resultFilePath = ".." + mascotArchivePath + "/" + mascotResultDate + "/" + mascotResultFilenamePrefix + mascotSearchNumberStr + ".dat";
    1114     log.debug("resultFilePath = \"" + resultFilePath + "\"");
    1115     //
    1116     DbControl dc = sc.newDbControl();
    1117     ItemFactory factory = new ItemFactory(dc);
    1118     // Hash table with default settings
    1119     Hashtable<String, String> mascotResultExportArgHashtable = fetchMascotResultExportDefaultSettings();
    1120     // Apache HttpClient stuff
    1121     PostMethod postMethod = null;
    1122     // HttpClient httpClient = null;
    1123     List<Part> partsList = new ArrayList<Part>(0);
    1124     String name = null;
    1125     String value = null;
    1126     // Prepare HTTP post
    1127     try
    1128     {
    1129       // Mascot result file to download
    1130       name = new String("file");
    1131       // value = new String("../data/20080924/F009765.dat");
    1132       value = resultFilePath;
    1133       log.debug("name = \"" + name + "\", value = \"" + value + "\"");
    1134       partsList.add(new StringPart(name, value));
    1135       log
    1136         .debug("partsList.get(" + (partsList.size() - 1) + ") = " + partsList
    1137           .get(partsList.size() - 1));
    1138       // Default settings
    1139       for (String argName : mascotResultExportArgHashtable.keySet())
    1140       {
    1141         name = argName;
    1142         value = mascotResultExportArgHashtable.get(argName);
    1143         log
    1144           .debug("name = \"" + argName + "\", value = \"" + value + "\"");
    1145         partsList.add(new StringPart(name, value));
    1146         log
    1147           .debug("partsList.get(" + (partsList.size() - 1) + ") = " + partsList
    1148             .get(partsList.size() - 1));
    1149       }
    1150       // Extra arguments
    1151       for (String argName : extraArgHashtable.keySet())
    1152       {
    1153         name = argName;
    1154         value = extraArgHashtable.get(argName);
    1155         log
    1156           .debug("name = \"" + argName + "\", value = \"" + value + "\"");
    1157         partsList.add(new StringPart(name, value));
    1158         log
    1159           .debug("partsList.get(" + (partsList.size() - 1) + ") = " + partsList
    1160             .get(partsList.size() - 1));
    1161       }
    1162       //
    1163       // Parts list is converted to an array one element
    1164       // at a time, to avoid casting problems with
    1165       // (Part[]) partsList.toArray().
    1166       //
    1167       Part[] parts = new Part[partsList.size()];
    1168       for (int i = 0; i < partsList.size(); i++)
    1169       {
    1170         parts[i] = (Part) partsList.get(i);
    1171       }
    1172       log.debug("parts.length = " + parts.length);
    1173       postMethod = new PostMethod(mascotExportScriptURLStr);
    1174       postMethod.setRequestEntity(new MultipartRequestEntity(parts,
    1175         postMethod.getParams()));
    1176     }
    1177     catch (Exception e)
    1178     {
    1179       log.debug("Exception when trying to get HttpClient items : " + e);
    1180     }
    1181     // Execute request
    1182     // String content = null;
    1183     String resultFilename = null;
    1184     String usedResultFilename = null;
    1185     // String inputDataFilename = new String("unknown");
    1186     boolean imported = false;
    1187     String ioErrorString = null;
    1188     log.debug("resultFilePath = \"" + resultFilePath + "\"");
    1189     String inputFilename = fetchMascotInputFilename(postMethod);
    1190     log.debug("inputFilename = \"" + inputFilename + "\"");
    1191     // If input filename not found, check for errors
    1192     String errorText = null;
    1193     if (inputFilename == null || inputFilename.equals(""))
    1194     {
    1195       errorText = checkMascotOutputForError(postMethod);
    1196     }
    1197     log.debug("errorText = \"" + errorText + "\"");
    1198     resultFilename = mascotResultFilenamePrefix + mascotSearchNumberStr + ".dat";
    1199     usedResultFilename = mascotResultDate + "_" + mascotResultFilenamePrefix + mascotSearchNumberStr + ".dat";
    1200     String filenameOfUploadedFile = constructFilenameOfUploadedFile(
    1201       inputFilename, usedResultFilename);
    1202     FileType fileType = factory.getBySystemId(FileType.class,
    1203       FileType.MASCOT_XML);
    1204     String description = new String(
    1205       "Mascot result - date: " + mascotResultDate + " file: " + resultFilename);
    1206     log.debug("resultFilename = \"" + resultFilename + "\"");
    1207     log.debug("usedResultFilename = \"" + usedResultFilename + "\"");
    1208     log
    1209       .debug("filenameOfUploadedFile = \"" + filenameOfUploadedFile + "\"");
    1210     log.debug("fileType = \"" + fileType + "\"");
    1211     log.debug("description = \"" + description + "\"");
    1212     String doneMessage = new String("");
    1213     String doneMessagePart2 = new String("");
    1214     // Only upload file if no error indicated
    1215     File resultFile = null;
    1216     if (errorText == null || errorText.equals(""))
    1217     {
    1218       try
    1219       {
    1220         resultFile = uploadFileViaWebInterface(postMethod, filenameOfUploadedFile,
    1221           dirId, description, fileType);
    1222         doneMessagePart2 = doneMessagePart2 + "\n" + "Result file uploaded: " + filenameOfUploadedFile;
    1223         imported = true;
    1224       }
    1225       catch (BaseException e)
    1226       {
    1227         log.warn(e);
    1228         doneMessagePart2 = doneMessagePart2 + "\n" + "Result file upload error: " + e;
    1229       }
    1230     }
    1231     else
    1232     {
    1233       doneMessagePart2 = doneMessagePart2 + "\n" + "Result file upload error: " + errorText;
    1234     }
    1235     //
    1236     if (imported)
    1237     {
    1238       doneMessage = new String("Mascot result retrieval finished");
    1239       doneMessage = doneMessage + "\n" + "Mascot result file: " + resultFilename;
    1240       doneMessage = doneMessage + doneMessagePart2;
    1241       response.setDone(doneMessage);
    1242     }
    1243     else
    1244     {
    1245       ArrayList<Throwable> err = new ArrayList<Throwable>(1);
    1246       // Construct job error message
    1247       String errorMessage = new String("Nothing retrieved");
    1248       // Add Mascot data input filename
    1249       errorMessage = errorMessage + "\n" + "Mascot result file path: " + resultFilePath;
    1250       //
    1251       if (ioErrorString != null && !ioErrorString.equals(""))
    1252       {
    1253         errorMessage = errorMessage + "\n" + ioErrorString;
    1254       }
    1255       errorMessage = errorMessage + "\n" + doneMessagePart2;
    1256       //
    1257       err.add(new BaseException(
    1258         "No Mascot result retrieval could be performed"));
    1259       response.setError(errorMessage, err);
    1260     }
    1261     log.debug("End of method.");
    1262     return resultFile;
    1263   }
    1264 
    1265 
    1266   /**
    1267    * Fetches Mascot result export default settings in the form of a hash table
    1268    * with name-value pairs.
    1269    *
    1270    * @return Hashtable<String, String> Name-value pair hash table of Mascot
    1271    *         result export default settings.
    1272    */
    1273   private Hashtable<String, String> fetchMascotResultExportDefaultSettings()
    1274   {
    1275     // Hash table with default settings
    1276     Hashtable<String, String> mascotResultExportArgHashtable = new Hashtable<String, String>(
    1277       0);
    1278     mascotResultExportArgHashtable.put("export_format", "XML");
    1279     mascotResultExportArgHashtable.put("do_export", "1");
    1280     // report : Number of hits to be reported (0 = AUTO)
    1281     mascotResultExportArgHashtable.put("report", "0");
    1282     // search_master=1 is necessary for any show_* to work
    1283     mascotResultExportArgHashtable.put("search_master", "1");
    1284     mascotResultExportArgHashtable.put("show_header", "1");
    1285     mascotResultExportArgHashtable.put("show_decoy", "0");
    1286     mascotResultExportArgHashtable.put("show_mods", "1");
    1287     mascotResultExportArgHashtable.put("show_params", "1");
    1288     mascotResultExportArgHashtable.put("show_format", "0");
    1289     mascotResultExportArgHashtable.put("show_masses", "0");
    1290     // show_unassigned : Include peptides not assigned to proteins
    1291     // show_inassigned settings excluded here as it is user-selected
    1292     // mascotResultExportArgHashtable.put("show_unassigned", "0");
    1293     mascotResultExportArgHashtable.put("show_same_sets", "1");
    1294     mascotResultExportArgHashtable.put("_showsubsets", "1");
    1295     // protein_master=1 is necessary for any prot_* to work
    1296     mascotResultExportArgHashtable.put("protein_master", "1");
    1297     mascotResultExportArgHashtable.put("prot_desc", "1");
    1298     mascotResultExportArgHashtable.put("prot_score", "1");
    1299     mascotResultExportArgHashtable.put("prot_thresh", "1");
    1300     mascotResultExportArgHashtable.put("prot_expect", "1");
    1301     mascotResultExportArgHashtable.put("prot_mass", "1");
    1302     mascotResultExportArgHashtable.put("prot_matches", "1");
    1303     mascotResultExportArgHashtable.put("prot_cover", "0");
    1304     mascotResultExportArgHashtable.put("prot_len", "0");
    1305     mascotResultExportArgHashtable.put("prot_pi", "1");
    1306     mascotResultExportArgHashtable.put("prot_empai", "1");
    1307     mascotResultExportArgHashtable.put("prot_tax_str", "0");
    1308     mascotResultExportArgHashtable.put("prot_tax_id", "0");
    1309     mascotResultExportArgHashtable.put("prot_seq", "0");
    1310     // peptide_master=1 is necessary for any pep_* to work
    1311     mascotResultExportArgHashtable.put("peptide_master", "1");
    1312     mascotResultExportArgHashtable.put("pep_exp_mr", "1");
    1313     mascotResultExportArgHashtable.put("pep_exp_z", "1");
    1314     mascotResultExportArgHashtable.put("pep_calc_mr", "1");
    1315     mascotResultExportArgHashtable.put("pep_delta", "1");
    1316     mascotResultExportArgHashtable.put("pep_start", "1");
    1317     mascotResultExportArgHashtable.put("pep_end", "1");
    1318     mascotResultExportArgHashtable.put("pep_miss", "1");
    1319     mascotResultExportArgHashtable.put("pep_score", "1");
    1320     mascotResultExportArgHashtable.put("pep_homol", "1");
    1321     mascotResultExportArgHashtable.put("pep_ident", "1");
    1322     mascotResultExportArgHashtable.put("pep_expect", "1");
    1323     mascotResultExportArgHashtable.put("pep_seq", "1");
    1324     mascotResultExportArgHashtable.put("pep_frame", "0");
    1325     mascotResultExportArgHashtable.put("pep_var_mod", "1");
    1326     mascotResultExportArgHashtable.put("pep_num_match", "0");
    1327     mascotResultExportArgHashtable.put("pep_scan_title", "1");
    1328     mascotResultExportArgHashtable.put("query_master", "1");
    1329     mascotResultExportArgHashtable.put("query_raw", "1");
    1330     // Return HashTable with Mascot result export default settings
    1331     return mascotResultExportArgHashtable;
    1332   }
    1333 
    1334 
    1335   /**
    1336    * Construct filename for uploaded search result file. The constructed
    1337    * filename consists of the (possibly modified) base name of the input file,
    1338    * an underscore, plus the modified result filename consisting of the date,
    1339    * an underscore, and the result filename with file extension ".dat"
    1340    * exchanged for ".xml".
    1341    *
    1342    * @param inputFilename String Filename of input file for search.
    1343    * @param filename String Filename of the search result file to upload.
    1344    * @return String The constructed filename for the uploaded result file.
    1345    */
    1346   private String constructFilenameOfUploadedFile(String inputFilename,
    1347       String filename)
    1348   {
    1349     String tmpStr;
    1350     if (inputFilename != null)
    1351     {
    1352       int strIndex;
    1353       // Construct filename to use for uploaded result file
    1354       tmpStr = inputFilename;
    1355       log.debug("tmpStr from input filename = \"" + tmpStr + "\"");
    1356       // Remove optional prefix ending with '\'
    1357       strIndex = tmpStr.lastIndexOf("\\");
    1358       if (strIndex >= 0)
    1359       {
    1360         tmpStr = tmpStr.substring(strIndex + 1);
    1361       }
    1362       log
    1363         .debug("tmpStr after removing prefix ending with '\\' = \"" + tmpStr + "\"");
    1364       // Remove optional prefix ending with '/'
    1365       strIndex = tmpStr.lastIndexOf("/");
    1366       if (strIndex >= 0)
    1367       {
    1368         tmpStr = tmpStr.substring(strIndex + 1);
    1369       }
    1370       log
    1371         .debug("tmpStr after removing prefix ending with '/' = \"" + tmpStr + "\"");
    1372       // Remove optional prefix ending with ':'
    1373       strIndex = tmpStr.lastIndexOf(":");
    1374       if (strIndex >= 0)
    1375       {
    1376         tmpStr = tmpStr.substring(strIndex + 1);
    1377       }
    1378       log
    1379         .debug("tmpStr after removing prefix ending with ':' = \"" + tmpStr + "\"");
    1380       // Remove any initial blanks
    1381       tmpStr = tmpStr.replaceAll("^\\ *", "");
    1382       log
    1383         .debug("tmpStr after removing initial blanks = \"" + tmpStr + "\"");
    1384       // Remove optional file extension from input filename
    1385       strIndex = tmpStr.lastIndexOf(".");
    1386       if (strIndex >= 0)
    1387       {
    1388         tmpStr = tmpStr.substring(0, strIndex);
    1389       }
    1390       log
    1391         .debug("tmpStr after removing file extension = \"" + tmpStr + "\"");
    1392       // Replace any remaining blanks ' ' in input filename with
    1393       // underscores '_'
    1394       tmpStr = tmpStr.replaceAll(" ", "_");
    1395       log
    1396         .debug("tmpStr after replacing blanks ' ' with underscores '_' = \"" + tmpStr + "\"");
    1397       // Replace any remaining dots '.' in input filename with underscores
    1398       // '_'
    1399       tmpStr = tmpStr.replaceAll("\\.", "_");
    1400       log
    1401         .debug("tmpStr after replacing dots '.' with underscores '_' = \"" + tmpStr + "\"");
    1402       // Add underscore and modified result filename
    1403       tmpStr = tmpStr + "_" + filename;
    1404       log
    1405         .debug("tmpStr after adding underscore '_' and modified result filename = \"" + tmpStr + "\"");
    1406     }
    1407     else
    1408     {
    1409       tmpStr = filename;
    1410     }
    1411     // Remove optional ".dat" file extension from result filename
    1412     int dotIndex = tmpStr.indexOf(".dat");
    1413     if (dotIndex >= 0)
    1414     {
    1415       tmpStr = tmpStr.substring(0, dotIndex);
    1416     }
    1417     // Add "xml" file extension
    1418     tmpStr = tmpStr + ".xml";
    1419     return tmpStr;
    1420   }
    1421 
    1422 
    1423   /**
    1424    * Returns a string padded from the left with a given character to have at
    1425    * least a given length. If the input string length that is equal to or
    1426    * larger than the minimum length, the string will be returned unchanged. If
    1427    * the input string length is smaller than the minimum length, the string
    1428    * will be padded from the left with the given character until the length is
    1429    * exactly the given minimum length.
    1430    *
    1431    * @param inStr String Input string
    1432    * @param minChars int The minimum number of characters in the resulting
    1433    *        string
    1434    * @param padChar char Character to use for optional padding
    1435    * @return String A string optionally padded from the left to have at least
    1436    *         'minChars' characters.
    1437    */
    1438   private String leftPaddedString(String inStr, int minChars, char padChar)
    1439   {
    1440     String startStr = null;
    1441     if (inStr != null)
    1442     {
    1443       startStr = new String(inStr);
    1444     }
    1445     else
    1446     {
    1447       startStr = new String("");
    1448     }
    1449     int startLen = startStr.length();
    1450     String resultStr = null;
    1451     if (startLen >= minChars)
    1452     {
    1453       resultStr = startStr;
    1454     }
    1455     else
    1456     {
    1457       resultStr = startStr;
    1458       for (int i = startLen; i < minChars; i++)
    1459       {
    1460         resultStr = padChar + resultStr;
    1461       }
    1462     }
    1463     return resultStr;
    1464   }
    1465 
    1466 
    1467   /**
    1468    * Fetches Mascot input filename from parsed result file via web interface
    1469    * from HTTPClient PostMethod.
    1470    *
    1471    * @param postMethod PostMethod HTTPClient PostMethod for getting input
    1472    *        stream of Mascot result file.
    1473    * @return String Mascot input filename for result file, or null if not
    1474    *         found.
    1475    */
    1476   private String fetchMascotInputFilename(PostMethod postMethod)
    1477   {
    1478     String inputFilename = null;
    1479     try
    1480     {
    1481       // Get HTTP client
    1482       HttpClient httpClient = new HttpClient();
    1483       int statusCode = httpClient.executeMethod(postMethod);
    1484       // Check status code
    1485       log.debug("Response status code: " + statusCode);
    1486       if (statusCode != HttpStatus.SC_OK)
    1487       {
    1488         log
    1489           .warn("Error when trying to execute PostMethod: " + postMethod
    1490             .getStatusLine());
    1491       }
    1492       // Get response
    1493       // InputStream inStream = postMethod.getResponseBodyAsStream();
    1494       String content = postMethod.getResponseBodyAsString();
    1495       String regexStr = new String("<FILENAME>.+</FILENAME>");
    1496       log.debug("regex string = \"" + regexStr + "\"");
    1497       Pattern p = Pattern.compile(regexStr);
    1498       Matcher m = p.matcher(content);
    1499       if (m.find())
    1500       {
    1501         // Extract filename from XML tag
    1502         String hitString = m.group(0);
    1503         log.debug("hitString = \"" + hitString + "\"");
    1504         hitString = hitString.replaceAll("<FILENAME>", "");
    1505         log.debug("hitString = \"" + hitString + "\"");
    1506         hitString = hitString.replaceAll("</FILENAME>", "");
    1507         log.debug("hitString = \"" + hitString + "\"");
    1508         if (hitString != null)
    1509         {
    1510           inputFilename = hitString;
    1511         }
    1512       }
    1513       else
    1514       {
    1515         log
    1516           .debug("regex string \"" + regexStr + "\" not found in file contents.");
    1517       }
    1518     }
    1519     catch (Exception e)
    1520     {
    1521       log.error(e.getMessage(), e);
    1522     }
    1523     finally
    1524     {
    1525       // Release current connection to the connection pool once you are
    1526       // done
    1527       if (postMethod != null)
    1528       {
    1529         log.debug("Trying to release connection to PostMethod");
    1530         postMethod.releaseConnection();
    1531         log.debug("After trying to release connection to PostMethod");
    1532       }
    1533       else
    1534       {
    1535         log.debug("Connection to PostMethod already closed.");
    1536       }
    1537     }
    1538     return inputFilename;
    1539   }
    1540 
    1541 
    1542   /**
    1543    * Checks Mascot output via web interface from HTTPClient PostMethod for
    1544    * indications of problems.
    1545    *
    1546    * @param postMethod PostMethod HTTPClient PostMethod for getting input
    1547    *        stream of Mascot result file.
    1548    * @return String Error text to display, or null if no error found.
    1549    */
    1550   private String checkMascotOutputForError(PostMethod postMethod)
    1551   {
    1552     String errorText = null;
    1553     try
    1554     {
    1555       // Get HTTP client
    1556       HttpClient httpClient = new HttpClient();
    1557       int statusCode = httpClient.executeMethod(postMethod);
    1558       // Check status code
    1559       log.debug("Response status code: " + statusCode);
    1560       if (statusCode != HttpStatus.SC_OK)
    1561       {
    1562         log
    1563           .warn("Error when trying to execute PostMethod: " + postMethod
    1564             .getStatusLine());
    1565       }
    1566       // Get response
    1567       // InputStream inStream = postMethod.getResponseBodyAsStream();
    1568       boolean errorIndicated = false;
    1569       String content = postMethod.getResponseBodyAsString();
    1570       String regexStr = new String("Software error:");
    1571       log.debug("regex string = \"" + regexStr + "\"");
    1572       Pattern p = Pattern.compile(regexStr);
    1573       Matcher m = p.matcher(content);
    1574       if (m.find())
    1575       {
    1576         errorIndicated = true;
    1577       }
    1578       else
    1579       {
    1580         log
    1581           .debug("regex string \"" + regexStr + "\" not found in file contents.");
    1582       }
    1583       if (errorIndicated)
    1584       {
    1585         regexStr = new String("<pre>.+</pre>");
    1586         log.debug("regex string = \"" + regexStr + "\"");
    1587         p = Pattern.compile(regexStr);
    1588         m = p.matcher(content);
    1589         if (m.find())
    1590         {
    1591           // Extract error message from HTML tag
    1592           String hitString = m.group(0);
    1593           log.debug("hitString = \"" + hitString + "\"");
    1594           hitString = hitString.replaceAll("<pre>", "");
    1595           log.debug("hitString = \"" + hitString + "\"");
    1596           hitString = hitString.replaceAll("</pre>", "");
    1597           log.debug("hitString = \"" + hitString + "\"");
    1598           if (hitString != null)
    1599           {
    1600             errorText = hitString;
    1601           }
    1602         }
    1603         else
    1604         {
    1605           log
    1606             .debug("regex string \"" + regexStr + "\" not found in file contents.");
    1607         }
    1608         if (errorText == null || errorText.equals(""))
    1609         {
    1610           errorText = new String(
    1611             "Software error reported by Mascot server, but error text could not be retrieved.");
    1612         }
    1613       }
    1614     }
    1615     catch (Exception e)
    1616     {
    1617       log.error(e.getMessage(), e);
    1618     }
    1619     finally
    1620     {
    1621       // Release current connection to the connection pool once you are
    1622       // done
    1623       if (postMethod != null)
    1624       {
    1625         log.debug("Trying to release connection to PostMethod");
    1626         postMethod.releaseConnection();
    1627         log.debug("After trying to release connection to PostMethod");
    1628       }
    1629       else
    1630       {
    1631         log.debug("Connection to PostMethod already closed.");
    1632       }
    1633     }
    1634     return errorText;
    1635   }
    1636 
    1637 
    1638   /**
    1639    * Uploads file via web interface into Proteios from HTTPClient PostMethod.
    1640    *
    1641    * @param postMethod PostMethod HTTPClient PostMethod for getting input
    1642    *        stream of file to upload
    1643    * @param filenameOfUploadedFile String Filename to use for uploaded file
    1644    * @param dirId Integer Database id of directory to put file item in
    1645    * @param descr String Description for uploaded file item
    1646    * @param fileType FileType FileType for uploaded file item
    1647    * @return File The uploaded file if OK, otherwise null.
    1648    * @throws BaseException
    1649    */
    1650   private File uploadFileViaWebInterface(PostMethod postMethod,
    1651       String filenameOfUploadedFile, Integer dirId, String descr,
    1652       FileType fileType)
    1653       throws BaseException
    1654   {
    1655     log
    1656       .debug("filenameOfUploadedFile = \"" + filenameOfUploadedFile + "\"");
    1657     log.debug("dirId = " + dirId);
    1658     log.debug("descr = \"" + descr + "\"");
    1659     log.debug("fileType = \"" + fileType + "\"");
    1660     /*
    1661      * If there is an active project the file is saved in the project
    1662      * directory, otherwise we use the users home directory.
    1663      */
    1664     DbControl dc = sc.newDbControl();
    1665     ItemFactory factory = new ItemFactory(dc);
    1666     //
    1667     Directory dir = null;
    1668     if (dirId != null && dirId > 0)
    1669     {
    1670       try
    1671       {
    1672         dir = factory.getById(Directory.class, dirId);
    1673       }
    1674       catch (Exception e1)
    1675       {
    1676         throw new BaseException(
    1677           "No such directory. Please contact administrator.");
    1678       }
    1679     }
    1680     log.debug("dir = " + dir);
    1681     //
    1682     File file = factory.create(File.class);
    1683     file.setDirectory(dir);
    1684     file.setName(filenameOfUploadedFile);
    1685     if (descr != null)
    1686     {
    1687       file.setDescription(descr);
    1688     }
    1689     file.setMimeType("text/xml");
    1690     file.setFileType(fileType);
    1691     dc.saveItem(file);
    1692     //
    1693     boolean fileUploadOK = false;
    1694     try
    1695     {
    1696       // Get HTTP client
    1697       HttpClient httpClient = new HttpClient();
    1698       int statusCode = httpClient.executeMethod(postMethod);
    1699       // Check status code
    1700       log.debug("Response status code: " + statusCode);
    1701       if (statusCode != HttpStatus.SC_OK)
    1702       {
    1703         log
    1704           .warn("Error when trying to execute PostMethod: " + postMethod
    1705             .getStatusLine());
    1706       }
    1707       // Get response
    1708       InputStream inStream = postMethod.getResponseBodyAsStream();
    1709       file.upload(inStream, false);
    1710       dc.commit();
    1711       inStream.close();
    1712       fileUploadOK = true;
    1713     }
    1714     catch (Exception e)
    1715     {
    1716       dc.close();
    1717       log.error(e.getMessage(), e);
    1718       throw new BaseException(e);
    1719     }
    1720     finally
    1721     {
    1722       // Release current connection to the connection pool once you are
    1723       // done
    1724       if (postMethod != null)
    1725       {
    1726         log.debug("Trying to release connection to PostMethod");
    1727         postMethod.releaseConnection();
    1728         log.debug("After trying to release connection to PostMethod");
    1729       }
    1730       else
    1731       {
    1732         log.debug("Connection to PostMethod already closed.");
    1733       }
    1734     }
    1735     //
    1736     File resultFile = null;
    1737     if (fileUploadOK)
    1738     {
    1739       resultFile = file;
    1740     }
    1741     return resultFile;
    1742971  }
    1743972
Note: See TracChangeset for help on using the changeset viewer.