Changeset 25054 for main


Ignore:
Timestamp:
2012-02-08T13:11:38+13:00 (12 years ago)
Author:
sjm84
Message:

Moved all of the unsecure code in LibraryServlet to a new FileLoaderServlet

Location:
main/trunk/greenstone3/src/java/org/greenstone/gsdl3
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/LibraryServlet.java

    r25053 r25054  
    356356    {
    357357        logUsageInfo(request);
    358 
    359         String query_string;
    360         if (request.getMethod().equals("GET"))
    361         {
    362             query_string = request.getQueryString();
    363         }
    364         else if (request.getMethod().equals("POST"))
    365         {
    366             query_string = "";
    367             Map paramMap = request.getParameterMap();
    368             Iterator keyIter = paramMap.keySet().iterator();
    369 
    370             while (keyIter.hasNext())
    371             {
    372                 String current = (String) keyIter.next();
    373                 query_string += current + "=" + ((String[]) paramMap.get(current))[0];
    374                 if (keyIter.hasNext())
    375                 {
    376                     query_string += "&";
    377                 }
    378             }
    379 
    380             DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
    381 
    382             int sizeLimit = System.getProperties().containsKey("servlet.upload.filesize.limit") ? Integer.parseInt(System.getProperty("servlet.upload.filesize.limit")) : 20 * 1024 * 1024;
    383 
    384             fileItemFactory.setSizeThreshold(sizeLimit);
    385             fileItemFactory.setRepository(new File(GlobalProperties.getGSDL3Home() + File.separator + "tmp"));
    386 
    387             ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
    388 
    389             String storageLocation = "";
    390             File uploadedFile = null;
    391             try
    392             {
    393                 List items = uploadHandler.parseRequest(request);
    394                 Iterator iter = items.iterator();
    395                 while (iter.hasNext())
    396                 {
    397                     FileItem current = (FileItem) iter.next();
    398                     if (current.isFormField())
    399                     {
    400                         query_string += current.getFieldName() + "=" + current.getString();
    401                         if (iter.hasNext())
    402                         {
    403                             query_string += "&";
    404                         }
    405 
    406                         if (current.getFieldName().equals(GSParams.FILE_LOCATION))
    407                         {
    408                             storageLocation = current.getString();
    409                         }
    410                     }
    411                     else
    412                     {
    413                         File file = new File(GlobalProperties.getGSDL3Home() + File.separator + "tmp" + File.separator + current.getName());
    414                         File tmpFolder = new File(GlobalProperties.getGSDL3Home() + File.separator + "tmp");
    415                         if (!tmpFolder.exists())
    416                         {
    417                             tmpFolder.mkdirs();
    418                         }
    419                         current.write(file);
    420 
    421                         uploadedFile = file;
    422                     }
    423                 }
    424 
    425                 if (!storageLocation.equals("") && uploadedFile != null)
    426                 {
    427                     String[] locations = storageLocation.split(":");
    428 
    429                     for (String location : locations)
    430                     {
    431                         File toFile = new File(GlobalProperties.getGSDL3Home() + location);
    432                         if (toFile.exists())
    433                         {
    434                             File backupFile = new File(toFile.getAbsolutePath() + System.currentTimeMillis());
    435 
    436                             logger.info("Backing up file (" + toFile.getAbsolutePath() + ") to " + backupFile.getAbsolutePath());
    437                             toFile.renameTo(backupFile);
    438                         }
    439 
    440                         FileChannel source = null;
    441                         FileChannel destination = null;
    442                         try
    443                         {
    444                             logger.info("Moving uploaded file (" + uploadedFile.getAbsolutePath() + ") to " + toFile.getAbsolutePath());
    445                             source = new FileInputStream(uploadedFile).getChannel();
    446                             destination = new FileOutputStream(toFile).getChannel();
    447                             destination.transferFrom(source, 0, source.size());
    448                         }
    449                         finally
    450                         {
    451                             if (source != null)
    452                             {
    453                                 source.close();
    454                             }
    455                             if (destination != null)
    456                             {
    457                                 destination.close();
    458                             }
    459                         }
    460 
    461                     }
    462                 }
    463             }
    464             catch (Exception e)
    465             {
    466                 logger.error("Exception in LibraryServlet -> " + e.getMessage());
    467             }
    468 
    469             if (query_string.equals(""))
    470             {
    471                 query_string = null;
    472             }
    473         }
    474         else
    475         {
    476             query_string = null;
    477         }
    478 
    479         if (query_string != null)
    480         {
    481             String[] query_arr = StringUtils.split(query_string, "&");
     358       
     359        Map<String,String[]> queryMap = request.getParameterMap();
     360        if (queryMap != null)
     361        {
     362            Iterator<String> queryIter = queryMap.keySet().iterator();
    482363            boolean redirect = false;
    483364            String href = null;
    484365            String rl = null;
    485366
    486             for (int i = 0; i < query_arr.length; i++)
    487             {
    488                 if (query_arr[i].startsWith("el="))
    489                 {
    490                     if (query_arr[i].substring(query_arr[i].indexOf("=") + 1, query_arr[i].length()).equals("direct"))
     367            while (queryIter.hasNext())
     368            {
     369                String q = queryIter.next();
     370                if (q.equals("el"))
     371                {
     372                    if ((queryMap.get(q)[0]).equals("direct"))
    491373                    {
    492374                        redirect = true;
    493375                    }
    494376                }
    495                 else if (query_arr[i].startsWith("href="))
    496                 {
    497                     href = query_arr[i].substring(query_arr[i].indexOf("=") + 1, query_arr[i].length());
     377                else if (q.equals("href"))
     378                {
     379                    href = queryMap.get(q)[0];
    498380                    href = StringUtils.replace(href, "%2f", "/");
    499381                    href = StringUtils.replace(href, "%7e", "~");
     
    501383                    href = StringUtils.replace(href, "%3A", "\\:");
    502384                }
    503                 else if (query_arr[i].startsWith("rl="))
    504                 {
    505                     rl = query_arr[i].substring(query_arr[i].indexOf("=") + 1, query_arr[i].length());
    506                 }
    507             }
    508 
    509             for (String arg : query_arr)
    510             {
    511                 if (arg.startsWith("downloadFile"))
    512                 {
    513                     int index = arg.indexOf("=");
    514                     if (index > -1 && index < arg.length() - 1)
    515                     {
    516                         String fileLocation = arg.substring(index + 1);
    517                         File fileToGet = new File(GlobalProperties.getGSDL3Home() + File.separator + fileLocation);
    518 
    519                         if (fileToGet.exists())
    520                         {
    521                             response.setContentType("application/octet-stream");
    522                             response.addHeader("Content-Disposition","attachment;filename=" + fileToGet.getName());
    523                             FileInputStream fis = new FileInputStream(fileToGet);
    524                             ServletOutputStream sos = response.getOutputStream();
    525 
    526                             byte[] buffer = new byte[4096];
    527                             int len;
    528                             while ((len = fis.read(buffer)) != -1)
    529                             {
    530                                 sos.write(buffer, 0, len);
    531                             }
    532                             sos.flush();
    533                             fis.close();
    534                             sos.close();
    535                            
    536                             return;
    537                         }
    538                     }
     385                else if (q.equals("rl"))
     386                {
     387                    rl = queryMap.get(q)[0];
    539388                }
    540389            }
Note: See TracChangeset for help on using the changeset viewer.