OSDN Git Service

ログインメソッドの追加.
authoryuki <yuki@bdf3b611-c98c-6041-8292-703d9c9adbe7>
Sat, 5 Jan 2008 09:05:23 +0000 (09:05 +0000)
committeryuki <yuki@bdf3b611-c98c-6041-8292-703d9c9adbe7>
Sat, 5 Jan 2008 09:05:23 +0000 (09:05 +0000)
git-svn-id: http://192.168.11.7/svn/repository/NicoBrowser@13 bdf3b611-c98c-6041-8292-703d9c9adbe7

nbproject/project.properties
src/nicobrowser/MainWindow.form
src/nicobrowser/MainWindow.java
test/nicobrowser/MainWindowTest.java [new file with mode: 0644]

index ee6e1e2..0130c53 100644 (file)
@@ -18,7 +18,10 @@ dist.javadoc.dir=${dist.dir}/javadoc
 excludes=\r
 includes=**\r
 jar.compress=false\r
-javac.classpath=\r
+javac.classpath=\\r
+    ${libs.HttpClient3.classpath}:\\r
+    ${libs.Log4J.classpath}:\\r
+    ${libs.Codec.classpath}\r
 # Space-separated list of extra javac options\r
 javac.compilerargs=\r
 javac.deprecation=false\r
@@ -27,7 +30,6 @@ javac.target=1.5
 javac.test.classpath=\\r
     ${javac.classpath}:\\r
     ${build.classes.dir}:\\r
-    ${libs.junit.classpath}:\\r
     ${libs.junit_4.classpath}\r
 javadoc.additionalparam=\r
 javadoc.author=false\r
@@ -40,6 +42,15 @@ javadoc.splitindex=true
 javadoc.use=true\r
 javadoc.version=false\r
 javadoc.windowtitle=\r
+# Property libs.Codec.classpath is set here just to make sharing of project simpler.\r
+# The library definition has always preference over this property.\r
+libs.Codec.classpath=../../../java/commons/commons-codec-1.3/commons-codec-1.3.jar\r
+# Property libs.HttpClient3.classpath is set here just to make sharing of project simpler.\r
+# The library definition has always preference over this property.\r
+libs.HttpClient3.classpath=../../../java/commons/commons-httpclient-3.1/commons-httpclient-3.1.jar\r
+# Property libs.Log4J.classpath is set here just to make sharing of project simpler.\r
+# The library definition has always preference over this property.\r
+libs.Log4J.classpath=../../../java/commons/commons-logging-1.1/commons-logging-adapters-1.1.jar;../../../java/commons/commons-logging-1.1/commons-logging-api-1.1.jar\r
 main.class=nicobrowser.MainWindow\r
 manifest.file=manifest.mf\r
 meta.inf.dir=${src.dir}/META-INF\r
index 201d6bf..f404790 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8" ?>
 
-<Form version="1.3" maxVersion="1.5" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
+<Form version="1.5" maxVersion="1.5" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
   <Properties>
     <Property name="defaultCloseOperation" type="int" value="3"/>
   </Properties>
           <Group type="103" groupAlignment="0" attributes="0">
               <Group type="102" alignment="0" attributes="0">
                   <EmptySpace max="-2" attributes="0"/>
-                  <Component id="jButton1" min="-2" max="-2" attributes="0"/>
+                  <Component id="loadButton" min="-2" max="-2" attributes="0"/>
                   <EmptySpace max="-2" attributes="0"/>
                   <Component id="jLabel1" min="-2" max="-2" attributes="0"/>
                   <EmptySpace max="-2" attributes="0"/>
-                  <Component id="jTextField1" linkSize="1" min="-2" max="-2" attributes="0"/>
+                  <Component id="mailField" linkSize="1" min="-2" max="-2" attributes="0"/>
                   <EmptySpace max="-2" attributes="0"/>
                   <Component id="jLabel2" min="-2" max="-2" attributes="0"/>
                   <EmptySpace max="-2" attributes="0"/>
-                  <Component id="jTextField2" linkSize="1" min="-2" pref="151" max="-2" attributes="0"/>
+                  <Component id="passwordField" linkSize="1" min="-2" pref="151" max="-2" attributes="0"/>
                   <EmptySpace max="32767" attributes="1"/>
               </Group>
           </Group>
               <Group type="102" alignment="0" attributes="0">
                   <EmptySpace max="-2" attributes="0"/>
                   <Group type="103" groupAlignment="3" attributes="0">
-                      <Component id="jButton1" alignment="3" min="-2" max="-2" attributes="0"/>
+                      <Component id="loadButton" alignment="3" min="-2" max="-2" attributes="0"/>
                       <Component id="jLabel1" alignment="3" min="-2" max="-2" attributes="0"/>
                       <Component id="jLabel2" alignment="3" min="-2" max="-2" attributes="0"/>
-                      <Component id="jTextField1" alignment="3" min="-2" max="-2" attributes="0"/>
-                      <Component id="jTextField2" alignment="3" min="-2" max="-2" attributes="0"/>
+                      <Component id="mailField" alignment="3" min="-2" max="-2" attributes="0"/>
+                      <Component id="passwordField" alignment="3" min="-2" max="-2" attributes="0"/>
                   </Group>
                   <EmptySpace max="32767" attributes="0"/>
               </Group>
         </DimensionLayout>
       </Layout>
       <SubComponents>
-        <Component class="javax.swing.JButton" name="jButton1">
+        <Component class="javax.swing.JButton" name="loadButton">
           <Properties>
             <Property name="text" type="java.lang.String" value="Load"/>
           </Properties>
+          <Events>
+            <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="loadButtonActionPerformed"/>
+          </Events>
         </Component>
         <Component class="javax.swing.JLabel" name="jLabel1">
           <Properties>
             <Property name="text" type="java.lang.String" value="password"/>
           </Properties>
         </Component>
-        <Component class="javax.swing.JTextField" name="jTextField1">
+        <Component class="javax.swing.JTextField" name="mailField">
         </Component>
-        <Component class="javax.swing.JTextField" name="jTextField2">
+        <Component class="javax.swing.JTextField" name="passwordField">
         </Component>
       </SubComponents>
     </Container>
index c345964..468839d 100644 (file)
@@ -5,7 +5,13 @@
  */
 package nicobrowser;
 
+import java.io.IOException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 import javax.swing.table.DefaultTableModel;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.cookie.CookiePolicy;
+import org.apache.commons.httpclient.methods.PostMethod;
 
 /**
  *
@@ -13,12 +19,15 @@ import javax.swing.table.DefaultTableModel;
  */
 public class MainWindow extends javax.swing.JFrame {
 
+    static final String LOGIN_PAGE = "https://secure.nicovideo.jp/secure/login?site=niconico";
     private static final String COLUMN_NAMES[] = {"サムネイル", "タイトル", "説明", "投稿日"};
     private DefaultTableModel tableModel = new DefaultTableModel(COLUMN_NAMES, 0);
+    HttpClient httpClient;
 
     /** Creates new form MainWindow */
     public MainWindow() {
         initComponents();
+        httpClient = new HttpClient();
     }
 
     /** This method is called from within the constructor to
@@ -32,11 +41,11 @@ public class MainWindow extends javax.swing.JFrame {
         jScrollPane1 = new javax.swing.JScrollPane();
         jTable1 = new javax.swing.JTable();
         jPanel1 = new javax.swing.JPanel();
-        jButton1 = new javax.swing.JButton();
+        loadButton = new javax.swing.JButton();
         jLabel1 = new javax.swing.JLabel();
         jLabel2 = new javax.swing.JLabel();
-        jTextField1 = new javax.swing.JTextField();
-        jTextField2 = new javax.swing.JTextField();
+        mailField = new javax.swing.JTextField();
+        passwordField = new javax.swing.JTextField();
 
         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
@@ -45,7 +54,12 @@ public class MainWindow extends javax.swing.JFrame {
 
         jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
 
-        jButton1.setText("Load");
+        loadButton.setText("Load");
+        loadButton.addActionListener(new java.awt.event.ActionListener() {
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                loadButtonActionPerformed(evt);
+            }
+        });
 
         jLabel1.setText("mail");
 
@@ -57,30 +71,30 @@ public class MainWindow extends javax.swing.JFrame {
             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGroup(jPanel1Layout.createSequentialGroup()
                 .addContainerGap()
-                .addComponent(jButton1)
+                .addComponent(loadButton)
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                 .addComponent(jLabel1)
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
-                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+                .addComponent(mailField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                 .addComponent(jLabel2)
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
-                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
+                .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
         );
 
-        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jTextField1, jTextField2});
+        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {mailField, passwordField});
 
         jPanel1Layout.setVerticalGroup(
             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGroup(jPanel1Layout.createSequentialGroup()
                 .addContainerGap()
                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
-                    .addComponent(jButton1)
+                    .addComponent(loadButton)
                     .addComponent(jLabel1)
                     .addComponent(jLabel2)
-                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
-                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
+                    .addComponent(mailField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
         );
 
@@ -107,6 +121,16 @@ public class MainWindow extends javax.swing.JFrame {
 
         pack();
     }// </editor-fold>//GEN-END:initComponents
+    private void loadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadButtonActionPerformed
+        String mail = mailField.getText();
+        String password = passwordField.getText();
+        if (mail.length() < 1 || password.length() < 1) {
+            //TODO エラー通知.
+            return;
+        }
+        login(mail, password);
+    }//GEN-LAST:event_loadButtonActionPerformed
+
     /**
      * @param args the command line arguments
      */
@@ -119,13 +143,42 @@ public class MainWindow extends javax.swing.JFrame {
         });
     }
     // Variables declaration - do not modify//GEN-BEGIN:variables
-    private javax.swing.JButton jButton1;
     private javax.swing.JLabel jLabel1;
     private javax.swing.JLabel jLabel2;
     private javax.swing.JPanel jPanel1;
     private javax.swing.JScrollPane jScrollPane1;
     private javax.swing.JTable jTable1;
-    private javax.swing.JTextField jTextField1;
-    private javax.swing.JTextField jTextField2;
+    private javax.swing.JButton loadButton;
+    private javax.swing.JTextField mailField;
+    private javax.swing.JTextField passwordField;
     // End of variables declaration//GEN-END:variables
+
+    /**
+     * ニコニコ動画へログインする.
+     * @param mail ログイン識別子(登録メールアドレス).
+     * @param password パスワード.
+     * @return 認証がOKであればtrue.
+     */
+    public boolean login(String mail, String password) {
+        boolean auth = false;
+        PostMethod post = new PostMethod(LOGIN_PAGE);
+
+        try {
+            post.addParameter("mail", mail);
+            post.addParameter("password", password);
+            post.addParameter("next_url", "");
+            post.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
+            httpClient.executeMethod(post);
+
+            // ログイン可否の判定.
+            // x-niconico-authflagで判定できそうだったが必ず0になる...
+            // Set-Cookieがあればログインできたとみなしているが,あまりよろしくないかも.
+            auth = (null != post.getResponseHeader("Set-Cookie")) ? true : false;
+        } catch (IOException ex) {
+            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
+        } finally {
+            post.releaseConnection();
+        }
+        return auth;
+    }
 }
diff --git a/test/nicobrowser/MainWindowTest.java b/test/nicobrowser/MainWindowTest.java
new file mode 100644 (file)
index 0000000..5a0ceaf
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package nicobrowser;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author yuki
+ */
+public class MainWindowTest {
+
+    public MainWindowTest() {
+    }
+
+    @BeforeClass
+    public static void setUpClass() throws Exception {
+    }
+
+    @AfterClass
+    public static void tearDownClass() throws Exception {
+    }
+
+    @Before
+    public void setUp() {
+    }
+
+    @After
+    public void tearDown() {
+    }
+
+    /**
+     * Test of login method, of class MainWindow.
+     */
+    @Test
+    public void login() {
+        System.out.println("login");
+        MainWindow instance = new MainWindow();
+
+        // ログイン失敗ケース
+        String mail = "test@example.com";
+        String password = "xxxx";
+        boolean result = instance.login(mail, password);
+        assertEquals(false, result);
+
+        // ログイン成功ケース
+        mail = "niconico.senyou@live.jp";
+        password = "piyopiyo";
+        result = instance.login(mail, password);
+        assertEquals(true, result);
+    }
+}