Package me.mabra.hellonzb.httpserver.nioengine

Source Code of me.mabra.hellonzb.httpserver.nioengine.HttpNzbFileReceiver

/*******************************************************************************
* 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.nioengine;

import me.mabra.hellonzb.AppConnector;
import me.mabra.hellonzb.httpserver.InvalidDataFormatException;
import me.mabra.hellonzb.httpserver.WebUploadParser;
import me.mabra.hellonzb.parser.NzbParser;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.util.CharsetUtil;

import java.io.IOException;


public class HttpNzbFileReceiver
{
  public HttpNzbFileReceiver()
  {
  }

  public void messageReceived(HttpServerHandler parentHandler, MessageEvent e, HttpRequest request) throws IOException
  {
    // evaluate request method (GET, POST, etc.)
    final String allowedMethod = "POST";
    HttpMethod requestMethod = request.getMethod();
    if(!requestMethod.toString().equalsIgnoreCase(allowedMethod))
    {
      parentHandler.invalidRequestMethod(e, allowedMethod);
      return;
    }

    // feed POST content to NZB parser
    ChannelBuffer content = request.getContent();
    if(content.readable())
    {
      WebUploadParser parser = new WebUploadParser(content.toString(CharsetUtil.UTF_8));
      try
      {
        parser.parse();
        NzbParser nzbParser = parser.getNzbParser();
        AppConnector._instance()._addNzbToQueue(nzbParser);

        parentHandler.redirect(e, "upload.py?upload=ok");
      }
      catch(InvalidDataFormatException ex)
      {
        AppConnector._instance()._getLogger().printStackTrace(ex);
        parentHandler.redirect(e, "upload.py?upload=err");
      }
      catch(Exception ex)
      {
        AppConnector._instance()._getLogger().printStackTrace(ex);
        parentHandler.redirect(e, "upload.py?upload=err");
      }
    }
  }
}
TOP

Related Classes of me.mabra.hellonzb.httpserver.nioengine.HttpNzbFileReceiver

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.