globalpresence.java

来自「ajax patterns 这是关于ajax设计模式方面的原代码」· Java 代码 · 共 71 行

JAVA
71
字号
/*
    Copyright 2006 Christian Gross http://www.devspace.com
    From the book Ajax Patterns and Best Practices

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package devspace.ajax.persistentcommunications;
import devspace.ajax.permutations.*;
import java.util.*;
import javax.servlet.http.*;
import devspace.testDrivenDevelopment.tracer.*;
import java.io.*;

public class GlobalPresence extends AuthenticatedHttpHandlerBase  {
    protected final String constUserIdentifier = "user-identifier";
    static String _buffer;
    static VersionTracker _version = new VersionTracker( 10000);
    static List<String> _users = new ArrayList<String>();
    private final String constUseAction = "user-action";
    
    public GlobalPresence() {
        assignUserResolver( new UserResolverImplementation());
    }
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        if (_version.getNew(req)) {
            synchronized (_version) {
                PrintWriter output = resp.getWriter();
                GenerateOutput.Write("GlobalPresence.DoGet", "Before writing users");
                for( String user : _users) {
                    output.write( "<b>" + user + "</b><br />");
                    GenerateOutput.Write("GlobalPresence.DoGet", "Writing user (" + user + ")");
                }
                GenerateOutput.Write("GlobalPresence.DoGet", "After writing users");
            }
            _version.writeVersion( resp);
            GenerateOutput.Write("GlobalPresence.DoGet", "Writing something changed");
        }
        else {
            resp.setStatus( 408, "No change");
            GenerateOutput.Write("GlobalPresence.DoGet", "Writing nothing changed");
        }
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        synchronized( _version) {
            String action = GetVariable( req, constUseAction);
            if( action.compareTo("add") == 0) {
                GenerateOutput.Write("GlobalPresence.DoPost", "Added a user (" + GetVariable( req, constUserIdentifier) + ")");
                _users.add( GetVariable( req, constUserIdentifier));
            }
            else if( action.compareTo( "remove") == 0) {
                GenerateOutput.Write("GlobalPresence.DoPost", "Removed a user (" + GetVariable( req, constUserIdentifier) + ")");
                _users.remove( GetVariable( req, constUserIdentifier));
            }
            else {
                GenerateOutput.Write("GlobalPresence.DoPost", "Did nothing, not right!");
            }
        }
        _version.increment();
    }
}

⌨️ 快捷键说明

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