Пример #1
0
                def test(self, song):
                    #No Symbolic Links
                    if Files.isRegularFile(song, LinkOption.NOFOLLOW_LINKS):
                        # MP3, WAV, AAC only, for now.
                        file = String(song.toFile().toURI().toString())
                        ext = file.substring(file.length() - 4,
                                             file.length())  #.XYZ

                        if ext not in ('.wav', '.WAV', '.mp3', '.MP3', '.aac',
                                       '.AAC'):
                            return False
                        else:
                            #We are presuming that a file with the correct extension is actually
                            #of the format specified by the extension and a valid file.
                            #We will handle if is not, but for now, filter out the obvious bad eggs.
                            return True
Пример #2
0
class JythonJavaHttpRequest(BaseHttpRequest):
    """
    Wrapper class around a concrete HTTP request representation.
    
    The class contains information about the received request and
    also provided the means to send a response. Therefore, this
    class encapsulates and controls the entire HTTP exchange between
    client and server.
    
    This class is part of the official http-abstraction-API. It is
    intended to be used by the rest of the code, shielding it from
    the specific server implementation.
    
    """
    __response_code    = None
    __native_req       = None
    __request_uri_str  = None
    __request_headers  = None
    __response_headers = dict()
    
    def __init__(self, native_request):
        """
        Initialize request wrapper with the native request class.
        
        The BaseHttpRequest may be used anywhere, but is only created within
        the http-abstraction-API. Therefore, it is ok for it to get the
        native-request object passed into it here.
        
        @param native_request:  The request representation of the native server.
        @type native_request:   com.sun.net.httpserver.HttpExchange
        
        """
        self.__native_req      = native_request
    
    def setResponseCode(self, code):
        """
        Set the response code, such as 200, 404, etc.
        
        This is the code that is sent in the response from the server
        to the client.
                
        The method can be called multiple times with different values
        before any of the send methods are called.
        
        @param code:  HTTP response code
        @type code:   int
        
        """
        self.__response_code = code
        
    def setResponseBody(self, body):
        """
        Set the response body.
        
        This method may be called multiple times with different values.
        
        @param body:    The data that should be send in the response body.
        @type body:     string
        
        """
        self.__response_body = String(body)
        
    def setResponseHeader(self, name, value):
        """
        Set a header for this response.

        @param name:    Name of the header.
        @type name:     string

        @param value:   Value for the header.
        @type value:    string

        """
        self.__response_headers[name] = value
                    
    def setResponse(self, code, body):
        """
        Set response code and body in one function.

        Same as calling setResponseCode() and setResponseBody()
        separately.
        
        @param code:    HTTP response code
        @type code:     int

        @param body:    The data that should be send in the response body.
        @type body:     string
        
        """
        self.setResponseCode(code)
        self.setResponseBody(body)
    
    def getRequestProtocol(self):
        """
        Return the protocol of the request.
        
        @return:    Protocol of the request, such as "HTTP/1.1"
        @rtype:     string
        
        """
        return self.__native_req.getProtocol()
    
    def getRequestMethod(self):
        """
        Return the method of the request.

        @return:    Method of the request, such as "GET", "POST", etc.
        @rtype:     string
        
        """
        return self.__native_req.getRequestMethod().upper()

    def getRequestURI(self):
        """
        Return the full URI of the request.
        
        @return:    URI of the request, containing server, path
                    and query portion.
        @rtype:     string
        
        """
        if not self.__request_uri_str:
            self.__request_uri_str = self.__native_req.getRequestURI().toString()
        return self.__request_uri_str
    
    def getRequestPath(self):
        """
        Return only the path component of the URI.
        
        @return:    The path component of the URI.
        @rtype:     string
        
        """
        return self.__native_req.getRequestURI().getPath()
    
    def getRequestHeaders(self):
        """
        Return a dictionary with the request headers.
        
        Each header can have multiple entries, so this is a
        dictionary of lists.
        
        @return:    Dictionary containing a list of values for each header.
        @rtype:     dict
        
        """
        if not self.__request_headers:
            self.__request_headers = dict(self.__native_req.getRequestHeaders())
        return self.__request_headers
    
    def getRequestQuery(self):
        """
        Return only the query component of the URI.
        
        @return:    Query portion of the URI (the part after the first '?').
        @rtype:     string
        
        """
        return self.__native_req.getRequestURI().getQuery()
    
    def getRequestBody(self):
        """
        Return the body of the request message.
        
        Note that this is not very suitable for streaming or large message bodies
        at this point, since the entire message is read into a single string
        before it is returned to the client.
        
        @return:    Body of the request.
        @rtype:     string
        
        """
        buffered_reader = BufferedReader(InputStreamReader(self.__native_req.getRequestBody()));
        lines = []
        while True:
            line = buffered_reader.readLine()
            if not line:
                break
            lines.append(line)
        
        return '\n'.join(lines)
    
    def sendResponseHeaders(self):
        """
        Send the previously specified response headers and code.
        
        """
        response_headers = self.__native_req.getResponseHeaders()
        for name, value in self.__response_headers.items():
            response_headers[name] = [ value ]
        self.__native_req.sendResponseHeaders(self.__response_code, self.__response_body.length())
    
    def sendResponseBody(self):
        """
        Send the previously specified request body.
        
        """
        os = DataOutputStream(self.__native_req.getResponseBody())
        os.writeBytes(self.__response_body)
        os.flush()
        os.close()
        
    def sendResponse(self):
        """
        Send the previously specified response headers, code and body.
        
        This is the same as calling sendResponseHeaders() and sendResponseBody()
        separately.
        
        """
        self.sendResponseHeaders()
        self.sendResponseBody()
        
    def close(self):
        """
        Close this connection.
        
        """
        self.__native_req.close()