001    /**
002     * Copyright 2007 Mike Kroutikov.
003     *
004     * This program is free software; you can redistribute it and/or modify
005     *   it under the terms of the Lesser GNU General Public License as 
006     *   published by the Free Software Foundation; either version 3 of
007     *   the License, or (at your option) any later version.
008     *
009     *   This program is distributed in the hope that it will be useful,
010     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
011     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     *   Lesser GNU General Public License for more details.
013     *
014     *   You should have received a copy of the Lesser GNU General Public License
015     *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
016     */
017    
018    package org.otfeed.protocol.request;
019    
020    
021    import org.otfeed.event.OTError;
022    import org.otfeed.protocol.CommandEnum;
023    import org.otfeed.protocol.VersionEnum;
024    import org.otfeed.protocol.OSEnum;
025    import org.otfeed.protocol.PlatformEnum;
026    
027    import java.nio.ByteBuffer;
028    
029    /**
030     * Request to login. Its the only one that does not have session part.
031     */
032    public class LoginRequest extends AbstractRequest {
033    
034            private final String username;
035            private final String password;
036    
037            public String getUsername() { return username; }
038            public String getPassword() { return password; }
039    
040            /**
041             * Represents response to the login request.
042             * Could be either re-direct or success.
043             */
044            public LoginRequest(int requestId,
045                            String u,
046                            String p) {
047    
048                    super(CommandEnum.LOGIN, requestId, null);
049    
050                    Check.notNull(u,  "username");
051                    Check.notNull(p,  "password");
052    
053                    username = u;
054                    password = p;
055            }
056    
057            public static class Response {
058    
059                    public final String sessionId;
060                    public final boolean redirectFlag;
061                    public final String redirectHost;
062                    public final int redirectPort;
063    
064                    public Response(ByteBuffer in) {
065                            sessionId = Util.readString(in, 64);
066                            redirectFlag = Util.readBoolean(in);
067                            redirectHost = Util.readString(in, 64);
068                            redirectPort = in.getShort();
069                    }
070    
071                    @Override
072                    public String toString() {
073                            return "Response: sessionId=" + sessionId
074                            + ", redirectFlag=" + redirectFlag
075                            + ", redirectHost=" + redirectHost
076                            + ", redirectPort=" + redirectPort;
077                    }
078            }
079    
080            @Override
081            public void writeRequest(ByteBuffer out) {
082                    super.writeRequest(out);
083    
084                    out.putShort((short) VersionEnum.CURRENT_VERSION.code);
085                    out.put((byte) OSEnum.UNKNOWN.code);
086                    out.put((byte) PlatformEnum.JAVA.code);
087                    Util.writeString(out, "", 16); // platform password .. skip
088                    out.put(new byte[6]);  // mac address... skip for now
089                    Util.writeString(out, username, 64);
090                    Util.writeString(out, password, 64);
091            }
092    
093            public void handleError(OTError error) {
094            }
095    }