if ( method == PUT ) {   tempFile = File.createTempFile( "exist", ".xml" );
BufferedWriter fout = new BufferedWriter(new FileWriter(tempFile));
    public void run(  ) {        String req;        String first;        String line;        String contentType = "text/xml";        String name = null;        String username = null;        String password = null;        int method = BAD_REQUEST;        int len = 0;        StringBuffer input = new StringBuffer(  );        File tempFile = null;        BrokerPool brokerPool = null;        try {            brokerPool = BrokerPool.getInstance(  );        } catch ( EXistException e ) {            throw new RuntimeException( e.getMessage(  ) );        }        // the main loop of this thread        while ( true ) {            while ( ( sock == null ) && !terminate )                synchronized ( this ) {                    try {                        wait( 500 );                    } catch ( InterruptedException ie ) {                    }                }            if ( terminate )                break;            try {                BufferedReader in =                    new BufferedReader( new InputStreamReader( sock                                                               .getInputStream(  )                                                               )                                        );                len = -1;                contentType = "text/xml";                // parse http-header                while ( true ) {                    req = in.readLine(  );                    if ( req == null )                        break;                    StringTokenizer tok = new StringTokenizer( req, " " );                    if ( !tok.hasMoreTokens(  ) )                        break;                    first = tok.nextToken(  );                    if ( first.equals( "GET" ) ) {                        method = GET;                        name = tok.nextToken(  );                    } else if ( first.equals( "POST" ) ) {                        method = POST;                        name = tok.nextToken(  );                    } else if ( first.equals( "HEAD" ) ) {                        method = HEAD;                        name = tok.nextToken(  );                    } else if ( first.equals( "PUT" ) ) {                        method = PUT;                        name = tok.nextToken(  );                    } else if ( req.toUpperCase(  ).startsWith( "CONTENT-LENGTH:" )                                ) {                        try {                            len = Integer.parseInt( tok.nextToken(  ) );                        } catch ( NumberFormatException nf ) {                            HttpServer.LOG.warn( nf );                            method = BAD_REQUEST;                            break;                        }                    } else if ( req.toUpperCase(  ).startsWith( "CONTENT-TYPE:" )                                )                        contentType = tok.nextToken(  );                    else if ( req.toUpperCase(  ).startsWith( "AUTHORIZATION:" )                              ) {                        String auth =                            req.substring( "AUTHORIZATION:".length(  ) ).trim(  );                        byte[] c =                            Base64.decode( auth.substring( 6 ).getBytes(  ) );                        String s = new String( c );                        int p = s.indexOf( ':' );                        username = s.substring( 0, p );                        password = s.substring( p + 1 );                        user = checkUser( brokerPool, username, password );                        if ( user == null )                            method = FORBIDDEN;                    }                }                // get request body                if ( len > 0 ) {                    char[] buffer = new char[2048];                    int count;                    int l = 0;                    if ( method == PUT ) {                        // put may send a lot of data, so save it                        // to a temporary file first.                        tempFile = File.createTempFile( "exist", ".xml" );                        BufferedWriter fout =                            new BufferedWriter( new FileWriter( tempFile ) );                        do {                            count = in.read( buffer );                            if ( count > 0 )                                fout.write( buffer, 0, count );                            l += count;                        } while ( l < len );                        fout.close(  );                    } else {                        do {                            count = in.read( buffer );                            if ( count > 0 )                                input.append( buffer, 0, count );                            l += count;                        } while ( l < len );                    }                }                if ( user == null )                    // no user specified: assume guest identity                    user = new User( "guest", "guest", "guest" );                try {                    broker = brokerPool.get(  );                    broker.setUser(user);                } catch ( EXistException e ) {                    throw new RuntimeException( e.getMessage(  ) );                }                try {                    // select method                    switch ( method ) {                    case FORBIDDEN:                        errorReply( FORBIDDEN );                        break;                    case BAD_REQUEST:                        errorReply( BAD_REQUEST );                        break;                    case GET:                        get( name, len );                        break;                    case POST:                        post( input.toString(  ), name, len, contentType );                        break;                    case PUT:                        put( tempFile, name, len );                        break;                    default:                        errorReply( BAD_REQUEST );                        break;                    }                } finally {                    brokerPool.release( broker );                    broker = null;                    user = null;                }                in.close(  );                input = new StringBuffer(  );            } catch ( IOException io ) {                HttpServer.LOG.warn( io );            }            sock = null;            xslStyle = null;            pool.release( this );        }        pool.release( this );    }