<?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>
*/
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;
/**
*
*/
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
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);
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");
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))
);
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
*/
});
}
// 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;
+ }
}
--- /dev/null
+/*
+ * 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);
+ }
+}