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

📄 restservice.java

📁 一个工作流设计及定义的系统,可以直接与数据库结合进行系统工作流程的定义及应用.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            }            int i = sAuth.indexOf(" ");            sAuth = sAuth.substring(i+1);            //            // base64 decoding (courtesy of Apache http://www.apache.org)            //log.debug("*** comment me ! *** sAuth is >"+sAuth+"<");            sAuth =                 new String(openwfe.org.misc.Base64.decode(sAuth.getBytes()));            //log.debug("*** comment me ! *** sAuth is >"+sAuth+"<");            //            // username:password            String[] ss = sAuth.split(":");            //            // set up work session            try            {                final Long sessionId = generateNewSessionId();                log.debug("authenticate() new sessionId : "+sessionId);                RestSession restSession =                     newRestSession(sessionId, ss[0], ss[1]);                ss = null;                this.sessions.put(sessionId, restSession);                //                // reply with session id                org.jdom.Element eSession = new org.jdom.Element("session");                eSession.setAttribute("id", ""+sessionId);                NetUtils.httpReply                    (key,                     200,                     "OK",                     this.serverName,                     null,                     "text/xml",                     eSession);                return;            }            catch (final Exception e)            {                exception = e;                log.debug("authenticate() failed.", e);                // fall to "401 Unauthorized"            }        }        NetUtils.httpReply            (key,             401,             "Unauthorized",             this.serverName,             new String[]              {                  "WWW-Authenticate: BASIC realm=\""+this.serverName+"\""              },             "text/plain",             exception);    }    protected RestSession newRestSession        (final Long sessionId, final String username, final String password)    throws        Exception    {        final Class clazz = Class.forName(this.restSessionClassName);        final RestSession session = (RestSession)clazz.newInstance();        session.init            (this,             sessionId,              username,              password);        log.debug("newRestSession() ok.");        return session;    }    private Long generateNewSessionId ()    {        synchronized (this.lastGivenId)        {            long id = System.currentTimeMillis();            while (id <= this.lastGivenId.longValue()) id++;            this.lastGivenId = new Long(id);            return this.lastGivenId;        }    }    private void timeOutSessions (long maxDelta)    {        log.debug("session cleaner waking up");        long now = System.currentTimeMillis();        synchronized (this.sessions)        {            java.util.List sessionsToRemove =                 new java.util.ArrayList(this.sessions.size());                        java.util.Iterator it = this.sessions.keySet().iterator();            while (it.hasNext())            {                Long sessionId = (Long)it.next();                RestSession session =                     (RestSession)this.sessions.get(sessionId);                final long delta = now - session.getLastUsed();                if (delta > maxDelta)                 {                    log.debug("Removing session "+sessionId);                    sessionsToRemove.add(sessionId);                }            }            //            // removing            it = sessionsToRemove.iterator();            while (it.hasNext()) this.sessions.remove(it.next());        }    }    protected void removeSession (Long sessionId)    {        synchronized (this.sessions)        {            this.sessions.remove(sessionId);        }    }    //    // STATIC METHODS    protected static Long extractSessionId (final byte[] request)        throws java.io.IOException    {        try        {            final String value = RestUtils.extractFromLine                (createBufferedReader(request).readLine(),                  "session");            log.debug("extractSessionId() sessionId = \""+value+"\"");            return new Long(Long.parseLong(value));        }        catch (Exception e)        {            //log.debug("extractSessionId() Failed to extract sessionId", e);            return null;        }    }    protected static String extractHeaderValue (byte[] request, String key)    {        //log.debug("Looking for key '"+key+"'");        java.io.BufferedReader br = createBufferedReader(request);        String line = null;        while (true)        {            try            {                line = br.readLine();            }            catch (java.io.IOException ie)            {                log.debug("failed to parse request headers", ie);                return null;            }            //log.debug("line is >"+line+"<");            if (line == null) return null;            if (line.equals("")) return null;            if (line.startsWith(key+": "))            {                return line.substring(key.length()+2);            }        }    }    public static java.io.BufferedReader createBufferedReader         (final byte[] request)    {        return            new java.io.BufferedReader                (new java.io.InputStreamReader                    (new java.io.ByteArrayInputStream(request)));    }    public static byte[] extractRequest (final SocketChannel channel)        throws java.io.IOException    {        //return NetUtils.channelToByteArray((ReadableByteChannel)channel);                return NetUtils.readHttpRequest((ReadableByteChannel)channel);    }    private static String print (final String[] ss)    {        StringBuffer sb = new StringBuffer();        sb.append("[");        for (int i=0; i<ss.length; i++)        {            sb.append("'");            sb.append(ss[i]);            sb.append("'");            if (i < ss.length-1) sb.append(", ");        }        sb.append("]");        return sb.toString();    }    /*     * verify first line     */    private boolean verify         (final SelectionKey key, final byte[] request)    {        if (request == null)        {            log.debug("null request");            return false;        }        String firstLine = "";        try        {            firstLine = createBufferedReader(request).readLine();        }        catch (java.io.IOException ie)        {            // ignore (ss will have 0 length)        }        String[] ss = firstLine.split(" ");        log.debug("ss is "+print(ss));        if (ss.length < 3)        {            NetUtils.httpReply                (key,                 400,                 "Bad request",                 this.serverName,                 null,                 "text/plain",                 "Incomplete HTTP request");            return false;        }        //        // check method        String method = ss[0].toUpperCase();        if ( ! method.equals("GET") && ! method.equals("POST"))        {            NetUtils.httpReply                (key,                 405,                 "Method Not Allowed",                 this.serverName,                 null,                 "text/plain",                 "Method '"+method+"' not allowed.");            return false;        }        //        // check URI                if ( ! ss[1].startsWith("/"+this.serverName))        {            NetUtils.httpReply                (key,                 404,                 "Not Found",                 this.serverName,                 null,                 "text/plain",                 "Object >"+ss[1]+"< not found.");            return false;        }        //        // check protocol                if ( ! ss[2].startsWith("HTTP/"))        {            NetUtils.httpReply                (key,                 505,                 "HTTP Version not supported",                 this.serverName,                 null,                 "text/plain",                 "HTTP Version >"+ss[2]+"< not supported.");            return false;        }        //        // any other check ?        return true;    }}

⌨️ 快捷键说明

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