Package me.mabra.hellonzb.httpserver

Source Code of me.mabra.hellonzb.httpserver.WebUploadParser

/*******************************************************************************
* HelloNzb -- The Binary Usenet Tool
* Copyright (C) 2010-2013 Matthias F. Brandstetter
* https://sourceforge.net/projects/hellonzb/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

package me.mabra.hellonzb.httpserver;

import me.mabra.hellonzb.parser.NzbParser;
import org.apache.commons.lang.StringUtils;

import javax.xml.stream.XMLStreamException;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;

public class WebUploadParser
{
  private enum State
  {
    INIT, FILENAME, TYPE, DATA
  };

  private State state;

  private String data;
  private String currId;
  private String currFilename;
  private String currData;

  private NzbParser nzbParser;


  public WebUploadParser(String data)
  {
    this.data = data;
    this.nzbParser = null;
    this.state = State.INIT;
  }

  public void parse() throws InvalidDataFormatException, IOException, XMLStreamException, ParseException
  {
    String[] dataLines = data.split("\n");

    // parse input data line by line
    for(String line : dataLines)
    {
      line = line.trim();

      switch(state)
      {
        case INIT:
          stateInit(line);
          break;
        case FILENAME:
          stateFilename(line);
          break;
        case TYPE:
          stateType(line);
          break;
        case DATA:
          stateData(line);
          break;
      }
    }
  }

  private void stateInit(String line) throws InvalidDataFormatException
  {
    StringBuffer id = new StringBuffer();

    int i = 0;
    for(; i < line.length() && line.charAt(i) == '-'; i++) ;
    for(; i < line.length(); i++)
      id.append(line.charAt(i));

    currId = id.toString();
    state = State.FILENAME;
  }

  private void stateFilename(String line) throws InvalidDataFormatException
  {
    final String prefix = "Content-Disposition: form-data; name=\"filename\"; filename=\"";
    StringBuffer filename = new StringBuffer();

    if(!line.startsWith(prefix))
      throw new InvalidDataFormatException("no (valid) filename header line found in data (id " + currId + ")");

    int i = prefix.length();
    for(; i < line.length() && line.charAt(i) != '"'; i++)
      filename.append(line.charAt(i));

    currFilename = filename.toString();
    if(currFilename.contains("/"))
      currFilename = currFilename.split("/")[StringUtils.countMatches(currFilename, "/")];
    if(currFilename.contains("\\"))
      currFilename = currFilename.split("\\\\")[StringUtils.countMatches(currFilename, "\\")];

    state = State.TYPE;
  }

  private void stateType(String line) throws InvalidDataFormatException
  {
    final String prefix1 = "Content-Type: application/";
    final String prefix2 = "Content-Type: text/xml";

    if(!line.startsWith(prefix1) && !line.startsWith(prefix2))
      throw new InvalidDataFormatException("found invalid content type: " + line);

    state = State.DATA;
  }

  private void stateData(String line) throws InvalidDataFormatException,
      IOException, XMLStreamException, ParseException
  {
    StringBuffer id = new StringBuffer();
    final String endPrefix = "-----";

    if(line.startsWith(endPrefix))
    {
      // end of data
      int i = 0;
      for(; i < line.length() && line.charAt(i) == '-'; i++) ;
      for(; i < line.length() && line.charAt(i) != '-'; i++)
        id.append(line.charAt(i));

      if(!currId.equals(id.toString()))
        throw new InvalidDataFormatException("corrupt or missing data (id missmatch " + currId + " != " + id.toString() + ")");

      // data ok, create new NzbParser for main app
      nzbParser = new NzbParser(writeToTmpFile(), null);
    }
    else
    {
      // new data line
      currData += line;
      currData += "\n";
    }
  }

  private String writeToTmpFile() throws IOException
  {
    String property = "java.io.tmpdir";
    String tempDir = System.getProperty(property);

    File dir = new File(tempDir);
    File filename = File.createTempFile(currFilename + "-", ".nzb", dir);

    FileWriter fileWriter = new FileWriter(filename, true);
    BufferedWriter bw = new BufferedWriter(fileWriter);
    bw.write(currData);
    bw.close();

    return filename.getCanonicalPath();
  }

  public NzbParser getNzbParser()
  {
    return nzbParser;
  }
}













































TOP

Related Classes of me.mabra.hellonzb.httpserver.WebUploadParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.