Changeset 4507
- Timestamp:
- Aug 15, 2013, 1:43:07 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 3 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/plugin/conf/xsd/mascot_search_results_2.xsd
r4262 r4507 2 2 <!-- 3 3 @(#)$Source: /vol/cvsroot/mascot/xml/schema/mascot_search_results_2.xsd,v $ 4 @(#)$Revision: 1.1 5$4 @(#)$Revision: 1.18 $ 5 5 @(#)$Name: $ 6 6 --> … … 435 435 </xs:annotation> 436 436 </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> 437 442 </xs:sequence> 438 443 </xs:complexType> … … 923 928 <xs:annotation> 924 929 <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> 925 940 </xs:annotation> 926 941 </xs:element> -
trunk/plugin/src/org/proteios/plugins/MascotResultRetrievalPlugin.java
r3860 r4507 28 28 package org.proteios.plugins; 29 29 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;30 import org.apache.http.HttpResponse; 31 import org.apache.http.HttpStatus; 32 import org.apache.http.client.methods.HttpGet; 33 import org.apache.http.client.methods.HttpPost; 34 import org.apache.http.entity.mime.MultipartEntity; 35 import org.apache.http.entity.mime.content.StringBody; 36 import org.apache.http.impl.client.BasicResponseHandler; 37 import org.apache.http.impl.client.DefaultHttpClient; 38 import org.apache.http.impl.client.LaxRedirectStrategy; 39 39 import org.proteios.core.BaseException; 40 40 import org.proteios.core.DbControl; … … 52 52 import org.proteios.core.plugin.Response; 53 53 54 import java.io.BufferedReader; 55 import java.io.FileInputStream; 56 import java.io.FileOutputStream; 57 import java.io.IOException; 54 import java.io.ByteArrayInputStream; 58 55 import java.io.InputStream; 59 import java.io.InputStreamReader; 60 import java.io.OutputStream; 61 import java.io.PrintWriter; 56 import java.io.UnsupportedEncodingException; 62 57 import java.util.ArrayList; 63 import java.util.Calendar;64 import java.util.GregorianCalendar;65 58 import java.util.Hashtable; 66 import java.util.List;67 59 import java.util.regex.Matcher; 68 60 import java.util.regex.Pattern; … … 118 110 .getValue("mascotExportScriptPath"); 119 111 // Get the Mascot result archive directory path 120 String mascotArchivePath = (String) job 121 .getValue("mascotArchivePath"); 112 String mascotArchivePath = (String) job.getValue("mascotArchivePath"); 122 113 // Get the Mascot result date 123 114 String mascotResultDateInput = (String) job … … 139 130 // Get id for the directory to upload the file to 140 131 Integer dirId = (Integer) job.getValue("dirId"); 141 // 132 // 142 133 int mascotSearchNumber = 0; 143 134 if (mascotSearchNumberInput != null) … … 166 157 mascotQueryPeaksStr = new String("1"); 167 158 } 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 169 161 String mascotQueryMasterStr = new String("0"); 170 162 if (mascotQueryPeaksInput != null && mascotQueryPeaksInput) … … 172 164 mascotQueryMasterStr = new String("1"); 173 165 } 174 // 166 // 175 167 log.debug("mascotServerURLStr = \"" + mascotServerURLStr + "\""); 176 168 log.debug("mascotExportScriptPath = \"" + mascotExportScriptPath + "\""); … … 189 181 log.debug("dirId = " + dirId); 190 182 // 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); 192 185 extraArgHashtable.put("show_unassigned", mascotShowUnassignedStr); 193 186 extraArgHashtable.put("query_master", mascotQueryMasterStr); 194 187 extraArgHashtable.put("query_peaks", mascotQueryPeaksStr); 195 188 // Perform Mascot result retrieval using web interface 196 mascotResultExportViaWebInterface(request, response, progress,197 mascotServerURLStr, mascotExportScriptPath,198 mascot ArchivePath, mascotResultDate,199 mascotResult FilenamePrefix, mascotSearchNumber,189 DbControl dc = sc.newDbControl(); 190 mascotResultExportViaWebInterface(dc, request, response, progress, 191 mascotServerURLStr, mascotExportScriptPath, mascotArchivePath, 192 mascotResultDate, mascotResultFilenamePrefix, mascotSearchNumber, 200 193 extraArgHashtable, dirId); 201 194 done(); … … 206 199 { 207 200 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"); 210 203 return about; 211 204 } … … 219 212 220 213 /** 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. 223 217 * 218 * @param dc DbControl to used for file creation 224 219 * @param request Request 225 220 * @param response Response … … 231 226 * @param mascotResultFilenamePrefix String Mascot result filename prefix 232 227 * @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. 234 230 * @param dirId Integer Id for the directory to upload the file to 235 231 */ 236 pr ivate void mascotResultExportViaWebInterface(Request request,237 Re sponse response, ProgressReporter progress,232 protected static File mascotResultExportViaWebInterface(DbControl dc, 233 Request request, Response response, ProgressReporter progress, 238 234 String mascotServerURLStr, String mascotExportScriptPath, 239 235 String mascotArchivePath, String mascotResultDate, … … 248 244 log.debug("mascotSearchNumber = " + mascotSearchNumber); 249 245 // Extra arguments 250 for (String argName : extraArgHashtable.keySet())246 for (String argName : extraArgHashtable.keySet()) 251 247 { 252 248 String argVal = extraArgHashtable.get(argName); … … 264 260 } 265 261 // 266 log 267 .debug("mascotExportScriptURLStr = \"" + mascotExportScriptURLStr + "\""); 268 log 269 .debug("mascotArchiveURLStr = \"" + mascotArchiveURLStr + "\""); 262 log.debug("mascotExportScriptURLStr = \"" + mascotExportScriptURLStr + "\""); 263 log.debug("mascotArchiveURLStr = \"" + mascotArchiveURLStr + "\""); 270 264 // 271 265 // "../data/20080924/F009765.dat"); 272 String mascotSearchNumberStr = leftPaddedString(Integer.toString(mascotSearchNumber), 6, '0'); 266 String mascotSearchNumberStr = leftPaddedString( 267 Integer.toString(mascotSearchNumber), 6, '0'); 273 268 log.debug("mascotSearchNumberStr = \"" + mascotSearchNumberStr + "\""); 274 269 String resultFilePath = ".." + mascotArchivePath + "/" + mascotResultDate + "/" + mascotResultFilenamePrefix + mascotSearchNumberStr + ".dat"; 275 270 log.debug("resultFilePath = \"" + resultFilePath + "\""); 276 271 // 277 DbControl dc = sc.newDbControl();278 ItemFactory factory = new ItemFactory(dc);279 272 // Hash table with default settings 280 273 Hashtable<String, String> mascotResultExportArgHashtable = fetchMascotResultExportDefaultSettings(); 281 274 // 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; 287 276 // Prepare HTTP post 277 postMethod = new HttpPost(mascotExportScriptURLStr); 278 MultipartEntity reqEntity = new MultipartEntity(); 288 279 try 289 280 { 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))); 309 286 } 310 287 // 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 340 301 // Execute request 341 String content = null;302 postMethod.setEntity(reqEntity); 342 303 String resultFilename = null; 343 304 String usedResultFilename = null; 344 String inputDataFilename = new String("unknown");345 boolean imported = false;346 String ioErrorString = null;347 305 log.debug("resultFilePath = \"" + resultFilePath + "\""); 348 String inputFilename = fetchMascotInputFilename(postMethod);349 log.debug("inputFilename = \"" + inputFilename + "\"");350 // If input filename not found, check for errors351 306 String errorText = null; 352 if (inputFilename == null || inputFilename.equals(""))353 {354 errorText = checkMascotOutputForError(postMethod);355 }356 307 log.debug("errorText = \"" + errorText + "\""); 357 308 resultFilename = mascotResultFilenamePrefix + mascotSearchNumberStr + ".dat"; 358 309 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); 362 312 log.debug("resultFilename = \"" + resultFilename + "\""); 363 313 log.debug("usedResultFilename = \"" + usedResultFilename + "\""); 364 log.debug("filenameOfUploadedFile = \"" + filenameOfUploadedFile + "\"");365 log.debug("fileType = \"" + fileType + "\"");366 314 log.debug("description = \"" + description + "\""); 367 String doneMessage = new String(""); 368 String doneMessagePart2 = new String(""); 315 File returnedFile = null; 369 316 // 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"); 393 322 doneMessage = doneMessage + "\n" + "Mascot result file: " + resultFilename; 394 doneMessage = doneMessage + doneMessagePart2;395 323 response.setDone(doneMessage); 396 324 } 397 else 398 { 325 catch (BaseException e) 326 { 327 String errorMessage = new String( 328 "Nothing retrieved\nResult file upload error: " + e); 399 329 ArrayList<Throwable> err = new ArrayList<Throwable>(1); 400 // Construct job error message401 String errorMessage = new String("Nothing retrieved");402 // Add Mascot data input filename403 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"));412 330 response.setError(errorMessage, err); 331 err.add(new BaseException( 332 "No Mascot result retrieval could be performed")); 413 333 } 414 334 log.debug("End of method."); 335 return returnedFile; 415 336 } 416 337 417 338 418 339 /** 419 * Fetches Mascot result export default settings 420 * in the form of a hash tablewith name-value pairs.340 * Fetches Mascot result export default settings in the form of a hash table 341 * with name-value pairs. 421 342 * 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. 423 345 */ 424 private Hashtable<String, String> fetchMascotResultExportDefaultSettings()346 private static Hashtable<String, String> fetchMascotResultExportDefaultSettings() 425 347 { 426 348 // 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); 428 351 mascotResultExportArgHashtable.put("export_format", "XML"); 429 352 mascotResultExportArgHashtable.put("do_export", "1"); 353 mascotResultExportArgHashtable.put("generate_file", "1"); 430 354 // report : Number of hits to be reported (0 = AUTO) 431 355 mascotResultExportArgHashtable.put("report", "0"); … … 440 364 // show_unassigned : Include peptides not assigned to proteins 441 365 // show_inassigned settings excluded here as it is user-selected 442 // mascotResultExportArgHashtable.put("show_unassigned", "0");366 // mascotResultExportArgHashtable.put("show_unassigned", "0"); 443 367 mascotResultExportArgHashtable.put("show_same_sets", "1"); 444 368 mascotResultExportArgHashtable.put("_showsubsets", "1"); … … 484 408 485 409 /** 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 modified489 * result filename consisting of the date, an underscore, and the490 * 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". 491 415 * 492 416 * @param inputFilename String Filename of input file for search. … … 494 418 * @return String The constructed filename for the uploaded result file. 495 419 */ 496 private String constructFilenameOfUploadedFile(String inputFilename,420 private static String constructFilenameOfUploadedFile(String inputFilename, 497 421 String filename) 498 422 { … … 535 459 } 536 460 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 '_' 538 463 tmpStr = tmpStr.replaceAll(" ", "_"); 539 464 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 // '_' 541 467 tmpStr = tmpStr.replaceAll("\\.", "_"); 542 468 log.debug("tmpStr after replacing dots '.' with underscores '_' = \"" + tmpStr + "\""); … … 576 502 * 'minChars' characters. 577 503 */ 578 private String leftPaddedString(String inStr, int minChars, char padChar) 504 private static String leftPaddedString(String inStr, int minChars, 505 char padChar) 579 506 { 580 507 String startStr = null; … … 606 533 607 534 /** 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. 610 536 * 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 774 539 * @param filenameOfUploadedFile String Filename to use for uploaded file 775 540 * @param dirId Integer Database id of directory to put file item in … … 778 543 * @throws BaseException 779 544 */ 780 private void uploadFileViaWebInterface(PostMethodpostMethod,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) 783 548 throws BaseException 784 549 { 785 log 786 .debug("filenameOfUploadedFile = \"" + filenameOfUploadedFile + "\""); 550 log.debug("filenameOfUploadedFile = \"" + filenameOfUploadedFile + "\""); 787 551 log.debug("dirId = " + dirId); 788 552 log.debug("descr = \"" + descr + "\""); 789 log.debug("fileType = \"" + fileType + "\"");790 553 /* 791 554 * If there is an active project the file is saved in the project 792 555 * directory, otherwise we use the users home directory. 793 556 */ 794 DbControl dc = sc.newDbControl(); 557 795 558 ItemFactory factory = new ItemFactory(dc); 559 FileType fileType = factory.getBySystemId(FileType.class, 560 FileType.MASCOT_XML); 561 log.debug("fileType = \"" + fileType + "\""); 562 796 563 // 797 564 Directory dir = null; … … 812 579 File file = factory.create(File.class); 813 580 file.setDirectory(dir); 814 file.setName(filenameOfUploadedFile); 581 815 582 if (descr != null) 816 583 { 817 584 file.setDescription(descr); 818 585 } 819 file.setMimeType(" text/xml");586 file.setMimeType("application/xml"); 820 587 file.setFileType(fileType); 821 588 dc.saveItem(file); 589 DefaultHttpClient httpClient = new DefaultHttpClient(); 822 590 // 823 591 try 824 592 { 825 593 // 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); 828 597 // 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 833 603 .getStatusLine()); 834 604 } 835 605 // 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); 837 662 file.upload(inStream, false); 838 663 dc.commit(); … … 859 684 log.debug("Connection to PostMethod already closed."); 860 685 } 861 } 686 httpClient.getConnectionManager().shutdown(); 687 } 688 return file; 862 689 } 863 690 } -
trunk/plugin/src/org/proteios/plugins/SearchMascotPlugin.java
r4237 r4507 39 39 import org.proteios.core.BaseException; 40 40 import org.proteios.core.DbControl; 41 import org.proteios.core.Directory;42 41 import org.proteios.core.File; 43 42 import org.proteios.core.FileParameterType; 44 import org.proteios.core.FileType;45 43 import org.proteios.core.ItemFactory; 46 44 import org.proteios.core.Job; … … 57 55 import org.proteios.io.MascotParameterSet; 58 56 59 import java.io.BufferedReader;60 57 import java.io.IOException; 61 58 import java.io.InputStream; 62 import java.io.InputStreamReader;63 import java.io.OutputStream;64 import java.io.PrintWriter;65 59 import java.util.ArrayList; 66 60 import java.util.Hashtable; … … 76 70 implements AbortablePluginInterface 77 71 { 78 private class StreamGobbler79 extends Thread80 {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 try115 {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.out131 .println(this.getClass().getSimpleName() + ": " + type + ">" + line);132 // Append text to content string133 if (content == null)134 {135 content = line;136 }137 else138 {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 }153 72 154 73 private class FileItemPartSource … … 448 367 About about = new AboutImpl("SearchMascotPlugin", 449 368 "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"); 451 370 return about; 452 371 } … … 997 916 extraArgHashtable.put("query_peaks", mascotQueryPeaksStr); 998 917 // Perform Mascot result retrieval using web interface 999 File resultFile = mascotResultExportViaWebInterface(request, response, progress,918 File resultFile = MascotResultRetrievalPlugin.mascotResultExportViaWebInterface(sc.newDbControl(), request, response, progress, 1000 919 mascotWebServerURLStr, mascotWebExportScriptPath, 1001 920 mascotWebArchivePath, mascotResultDate, … … 1025 944 } 1026 945 response.setDone(doneMessage); 1027 dc.commit();1028 946 } 1029 947 else … … 1051 969 } 1052 970 log.debug("End of method."); 1053 }1054 1055 1056 /**1057 * Mascot results XML export1058 *1059 * @param request Request1060 * @param response Response1061 * @param progress ProgressReporter1062 * @param mascotServerURLStr String URL to the Mascot server1063 * @param mascotExportScriptPath String Mascot result export script path1064 * @param mascotArchivePath String Mascot result archive directory path1065 * @param mascotResultDate String Result date on Mascot server1066 * @param mascotResultFilenamePrefix String Mascot result filename prefix1067 * @param mascotSearchNumber int Mascot search number1068 * @param extraArgHashtable Hashtable<String, String> Key-value pairs for1069 * extra arguments.1070 * @param dirId Integer Id for the directory to upload the file to1071 * @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 log1082 .debug("mascotExportScriptPath = \"" + mascotExportScriptPath + "\"");1083 log.debug("mascotArchivePath = \"" + mascotArchivePath + "\"");1084 log.debug("mascotResultDate = \"" + mascotResultDate + "\"");1085 log1086 .debug("mascotResultFilenamePrefix = \"" + mascotResultFilenamePrefix + "\"");1087 log.debug("mascotSearchNumber = " + mascotSearchNumber);1088 // Extra arguments1089 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 in1098 // messages1099 if (mascotServerURLStr.endsWith("/") || mascotArchivePath1100 .startsWith("/"))1101 {1102 mascotArchiveURLStr = mascotServerURLStr + mascotArchivePath;1103 }1104 //1105 log1106 .debug("mascotExportScriptURLStr = \"" + mascotExportScriptURLStr + "\"");1107 log.debug("mascotArchiveURLStr = \"" + mascotArchiveURLStr + "\"");1108 //1109 // "../data/20080924/F009765.dat");1110 String mascotSearchNumberStr = leftPaddedString(Integer1111 .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 settings1119 Hashtable<String, String> mascotResultExportArgHashtable = fetchMascotResultExportDefaultSettings();1120 // Apache HttpClient stuff1121 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 post1127 try1128 {1129 // Mascot result file to download1130 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 log1136 .debug("partsList.get(" + (partsList.size() - 1) + ") = " + partsList1137 .get(partsList.size() - 1));1138 // Default settings1139 for (String argName : mascotResultExportArgHashtable.keySet())1140 {1141 name = argName;1142 value = mascotResultExportArgHashtable.get(argName);1143 log1144 .debug("name = \"" + argName + "\", value = \"" + value + "\"");1145 partsList.add(new StringPart(name, value));1146 log1147 .debug("partsList.get(" + (partsList.size() - 1) + ") = " + partsList1148 .get(partsList.size() - 1));1149 }1150 // Extra arguments1151 for (String argName : extraArgHashtable.keySet())1152 {1153 name = argName;1154 value = extraArgHashtable.get(argName);1155 log1156 .debug("name = \"" + argName + "\", value = \"" + value + "\"");1157 partsList.add(new StringPart(name, value));1158 log1159 .debug("partsList.get(" + (partsList.size() - 1) + ") = " + partsList1160 .get(partsList.size() - 1));1161 }1162 //1163 // Parts list is converted to an array one element1164 // at a time, to avoid casting problems with1165 // (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 request1182 // 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 errors1192 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 log1209 .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 indicated1215 File resultFile = null;1216 if (errorText == null || errorText.equals(""))1217 {1218 try1219 {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 else1232 {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 else1244 {1245 ArrayList<Throwable> err = new ArrayList<Throwable>(1);1246 // Construct job error message1247 String errorMessage = new String("Nothing retrieved");1248 // Add Mascot data input filename1249 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 table1268 * with name-value pairs.1269 *1270 * @return Hashtable<String, String> Name-value pair hash table of Mascot1271 * result export default settings.1272 */1273 private Hashtable<String, String> fetchMascotResultExportDefaultSettings()1274 {1275 // Hash table with default settings1276 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 work1283 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 proteins1291 // show_inassigned settings excluded here as it is user-selected1292 // 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 work1296 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 work1311 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 settings1331 return mascotResultExportArgHashtable;1332 }1333 1334 1335 /**1336 * Construct filename for uploaded search result file. The constructed1337 * 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 file1354 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 log1363 .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 log1371 .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 log1379 .debug("tmpStr after removing prefix ending with ':' = \"" + tmpStr + "\"");1380 // Remove any initial blanks1381 tmpStr = tmpStr.replaceAll("^\\ *", "");1382 log1383 .debug("tmpStr after removing initial blanks = \"" + tmpStr + "\"");1384 // Remove optional file extension from input filename1385 strIndex = tmpStr.lastIndexOf(".");1386 if (strIndex >= 0)1387 {1388 tmpStr = tmpStr.substring(0, strIndex);1389 }1390 log1391 .debug("tmpStr after removing file extension = \"" + tmpStr + "\"");1392 // Replace any remaining blanks ' ' in input filename with1393 // underscores '_'1394 tmpStr = tmpStr.replaceAll(" ", "_");1395 log1396 .debug("tmpStr after replacing blanks ' ' with underscores '_' = \"" + tmpStr + "\"");1397 // Replace any remaining dots '.' in input filename with underscores1398 // '_'1399 tmpStr = tmpStr.replaceAll("\\.", "_");1400 log1401 .debug("tmpStr after replacing dots '.' with underscores '_' = \"" + tmpStr + "\"");1402 // Add underscore and modified result filename1403 tmpStr = tmpStr + "_" + filename;1404 log1405 .debug("tmpStr after adding underscore '_' and modified result filename = \"" + tmpStr + "\"");1406 }1407 else1408 {1409 tmpStr = filename;1410 }1411 // Remove optional ".dat" file extension from result filename1412 int dotIndex = tmpStr.indexOf(".dat");1413 if (dotIndex >= 0)1414 {1415 tmpStr = tmpStr.substring(0, dotIndex);1416 }1417 // Add "xml" file extension1418 tmpStr = tmpStr + ".xml";1419 return tmpStr;1420 }1421 1422 1423 /**1424 * Returns a string padded from the left with a given character to have at1425 * least a given length. If the input string length that is equal to or1426 * larger than the minimum length, the string will be returned unchanged. If1427 * the input string length is smaller than the minimum length, the string1428 * will be padded from the left with the given character until the length is1429 * exactly the given minimum length.1430 *1431 * @param inStr String Input string1432 * @param minChars int The minimum number of characters in the resulting1433 * string1434 * @param padChar char Character to use for optional padding1435 * @return String A string optionally padded from the left to have at least1436 * '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 else1446 {1447 startStr = new String("");1448 }1449 int startLen = startStr.length();1450 String resultStr = null;1451 if (startLen >= minChars)1452 {1453 resultStr = startStr;1454 }1455 else1456 {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 interface1469 * from HTTPClient PostMethod.1470 *1471 * @param postMethod PostMethod HTTPClient PostMethod for getting input1472 * stream of Mascot result file.1473 * @return String Mascot input filename for result file, or null if not1474 * found.1475 */1476 private String fetchMascotInputFilename(PostMethod postMethod)1477 {1478 String inputFilename = null;1479 try1480 {1481 // Get HTTP client1482 HttpClient httpClient = new HttpClient();1483 int statusCode = httpClient.executeMethod(postMethod);1484 // Check status code1485 log.debug("Response status code: " + statusCode);1486 if (statusCode != HttpStatus.SC_OK)1487 {1488 log1489 .warn("Error when trying to execute PostMethod: " + postMethod1490 .getStatusLine());1491 }1492 // Get response1493 // 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 tag1502 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 else1514 {1515 log1516 .debug("regex string \"" + regexStr + "\" not found in file contents.");1517 }1518 }1519 catch (Exception e)1520 {1521 log.error(e.getMessage(), e);1522 }1523 finally1524 {1525 // Release current connection to the connection pool once you are1526 // done1527 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 else1534 {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 for1544 * indications of problems.1545 *1546 * @param postMethod PostMethod HTTPClient PostMethod for getting input1547 * 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 try1554 {1555 // Get HTTP client1556 HttpClient httpClient = new HttpClient();1557 int statusCode = httpClient.executeMethod(postMethod);1558 // Check status code1559 log.debug("Response status code: " + statusCode);1560 if (statusCode != HttpStatus.SC_OK)1561 {1562 log1563 .warn("Error when trying to execute PostMethod: " + postMethod1564 .getStatusLine());1565 }1566 // Get response1567 // 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 else1579 {1580 log1581 .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 tag1592 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 else1604 {1605 log1606 .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 finally1620 {1621 // Release current connection to the connection pool once you are1622 // done1623 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 else1630 {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 input1642 * stream of file to upload1643 * @param filenameOfUploadedFile String Filename to use for uploaded file1644 * @param dirId Integer Database id of directory to put file item in1645 * @param descr String Description for uploaded file item1646 * @param fileType FileType FileType for uploaded file item1647 * @return File The uploaded file if OK, otherwise null.1648 * @throws BaseException1649 */1650 private File uploadFileViaWebInterface(PostMethod postMethod,1651 String filenameOfUploadedFile, Integer dirId, String descr,1652 FileType fileType)1653 throws BaseException1654 {1655 log1656 .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 project1662 * 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 try1671 {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 try1695 {1696 // Get HTTP client1697 HttpClient httpClient = new HttpClient();1698 int statusCode = httpClient.executeMethod(postMethod);1699 // Check status code1700 log.debug("Response status code: " + statusCode);1701 if (statusCode != HttpStatus.SC_OK)1702 {1703 log1704 .warn("Error when trying to execute PostMethod: " + postMethod1705 .getStatusLine());1706 }1707 // Get response1708 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 finally1721 {1722 // Release current connection to the connection pool once you are1723 // done1724 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 else1731 {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;1742 971 } 1743 972
Note: See TracChangeset
for help on using the changeset viewer.