⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httprequesthandler.java

📁 国外的j2me播放器软件
💻 JAVA
字号:
package no.auc.one.portableplayer.communication.upnphosting;

import java.util.Calendar;
import java.util.TimeZone;

import no.auc.one.portableplayer.utils.Utilities;

/**
 * Handles requests for a specific namespace of the server, such as /content.
 * XXX Should do something more appropriate in the "default case", like 
 *     returning some HTTP error indicating that the client's request 
 *     is not allowed/supported.
 */
public abstract class HttpRequestHandler {

    /**
     * List of methods this handler understands.
     * 
     * Must be overriden and give a comma separated list of values 
     * indicating which methods this handler understands. This list should 
     * be used when generating the Allow header.
     *  
     *  XXX This isn't exactly a very good way to support Allow header, since 
     *      it doesn't differentiate between the different resources in a 
     *      namespace. E.g. the /ssdp namespace allows GET for most 
     *      resoursces, but not POST.
     */ 
    public abstract String allowedMethods();

    /**
     * Handle GET requests.
     *
     * @param req Information about the request.
     *
     * @return The response to be returned back to the client. Do NOT use the 
     *         SocketConnection of the req parameter for this.
     *
     *         Currently no exceptions are allowed to be throwed. Later this 
     *         might be added to make it easier to take care of error 
     *         conditions. As of now there are two options:
     *         1) Return null, which is handled by the server code to 
     *            return a HTTP 501 Server Internal Error response back to 
     *            the client.
     *         2) Create the appropriate error response in the handler and 
     *            return it as a normal return value.
     *
     */
    public byte[] handleGetRequest(HttpRequest req) {
        StringBuffer sb = new StringBuffer(128);
        sb.append("HTTP/1.1 405 Method Not Allowed\r\n");
        sb.append(
            "Date: " + Utilities.getRfcDateString(
                Calendar.getInstance(TimeZone.getTimeZone("GMT"))));
        sb.append("Content-Length: 0\r\nConnection: close\r\n");
        sb.append("Allow: ");
        sb.append(allowedMethods());
        sb.append("\r\n\r\n");
        
        return sb.toString().getBytes();
    }

    /**
     * Handle POST requests.
     *
     * @param req Information about the request.
     *
     * @return The response to be returned back to the client. Do NOT use the 
     *         SocketConnection of the req parameter for this.
     */
    public byte[] handlePostRequest(HttpRequest req) {
        StringBuffer sb = new StringBuffer(128);
        sb.append("HTTP/1.1 405 Method Not Allowed\r\n");
        sb.append(
            "Date: " + Utilities.getRfcDateString(
                Calendar.getInstance(TimeZone.getTimeZone("GMT"))));
        sb.append("Content-Length: 0\r\nConnection: close\r\n");
        sb.append("Allow: ");
        sb.append(allowedMethods());
        sb.append("\r\n\r\n");
        
        return sb.toString().getBytes();
    }
    
    /**
     * Handle OPTIONS requests.
     *
     * @param req Information about the request.
     *
     * @return The response to be returned back to the client. Do NOT use the 
     *         SocketConnection of the req parameter for this.
     */
    public byte[] handleOptionsRequest(HttpRequest req) {
        System.err.println("Options request");
        
        StringBuffer sb = new StringBuffer(128);
        sb.append("HTTP/1.1 200 OK\r\n");
        sb.append(
            "Date: " + Utilities.getRfcDateString(
                Calendar.getInstance(TimeZone.getTimeZone("GMT"))));
        sb.append("\r\nContent-Length: 0\r\nConnection: close\r\n");
        sb.append("Allow: ");
        sb.append(allowedMethods());
        sb.append("\r\n\r\n");
        
        System.err.println("Response=\r\n" + sb.toString());
        
        return sb.toString().getBytes();
    }    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -