updated commons-codec

introduced password authorization dialog
actual sketch posting
This commit is contained in:
Federico Fissore 2013-04-16 16:15:12 +02:00
parent 4cbd1cf9fe
commit 078a6f5630
7 changed files with 191 additions and 21 deletions

Binary file not shown.

Binary file not shown.

View File

@ -829,6 +829,13 @@ public class Preferences {
*/
}
public static boolean has(String key) {
return table.containsKey(key);
}
public static void remove(String key) {
table.remove(key);
}
static public String getDefault(String attribute) {
return (String) defaults.get(attribute);

View File

@ -23,8 +23,10 @@
package processing.app;
import org.apache.commons.codec.digest.DigestUtils;
import processing.app.debug.*;
import processing.app.debug.Compiler;
import processing.app.forms.PasswordAuthorizationDialog;
import processing.app.helpers.PreferencesMap;
import processing.app.packages.Library;
import processing.app.packages.LibraryList;
@ -1598,9 +1600,9 @@ public class Sketch {
// }
editor.status.progressNotice(_("Uploading..."));
upload(appletPath, foundName, usingProgrammer);
boolean success = upload(appletPath, foundName, usingProgrammer);
editor.status.progressUpdate(100);
return true;
return success;
}
@ -1656,16 +1658,33 @@ public class Sketch {
System.out.println(_("Low memory available, stability problems may occur"));
}
protected String upload(String buildPath, String suggestedClassName, boolean usingProgrammer) throws RunnerException, SerialException {
protected boolean upload(String buildPath, String suggestedClassName, boolean usingProgrammer) throws RunnerException, SerialException {
TargetPlatform target = Base.getTargetPlatform();
String board = Preferences.get("board");
Uploader uploader = new UploaderFactory().newUploader(target.getBoards().get(board), Preferences.get("serial.port"));
if (uploader.requiresAuthorization() && !Preferences.has(uploader.getAuthorizationKey())) {
PasswordAuthorizationDialog dialog = new PasswordAuthorizationDialog(editor);
dialog.setLocationRelativeTo(editor);
dialog.setVisible(true);
if (dialog.isCancelled()) {
editor.statusNotice(_("Upload cancelled"));
return false;
}
Preferences.set(uploader.getAuthorizationKey(), DigestUtils.sha512Hex(dialog.getPassword()));
}
boolean success = uploader.uploadUsingPreferences(buildPath, suggestedClassName, usingProgrammer);
return success ? suggestedClassName : null;
if (uploader.requiresAuthorization() && !success) {
Preferences.remove(uploader.getAuthorizationKey());
}
return success;
}

View File

@ -6,6 +6,7 @@ import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import processing.app.Preferences;
import processing.app.SerialException;
import java.io.File;
@ -32,6 +33,14 @@ public class HttpUploader extends Uploader {
this.ipAddress = matcher.group(1);
}
public boolean requiresAuthorization() {
return true;
}
public String getAuthorizationKey() {
return "pwd." + ipAddress;
}
@Override
public boolean uploadUsingPreferences(String buildPath, String className, boolean usingProgrammer) throws RunnerException, SerialException {
if (usingProgrammer) {
@ -41,13 +50,14 @@ public class HttpUploader extends Uploader {
FilePart filePart;
try {
filePart = new FilePart("sketch.hex", new File(buildPath, className + ".hex"));
filePart = new FilePart("sketch", new File(buildPath, className + ".hex"));
} catch (FileNotFoundException e) {
throw new RunnerException(e);
}
Part[] parts = {filePart};
PostMethod post = newPostMethod();
post.setRequestHeader("Cookie", "pwd=" + Preferences.get(getAuthorizationKey()));
post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
int statusCode;
@ -58,7 +68,6 @@ public class HttpUploader extends Uploader {
}
if (statusCode == HttpStatus.SC_OK) {
System.out.println(_("Sketch uploaded"));
return true;
}
@ -71,7 +80,7 @@ public class HttpUploader extends Uploader {
}
protected PostMethod newPostMethod() {
return new PostMethod("http://" + ipAddress + ":8000/upload");
return new PostMethod("http://" + ipAddress + ":6571/upload");
}
@Override

View File

@ -56,6 +56,14 @@ public abstract class Uploader implements MessageConsumer {
public abstract boolean burnBootloader() throws RunnerException;
public boolean requiresAuthorization() {
return false;
}
public String getAuthorizationKey() {
return null;
}
protected void flushSerialBuffer() throws RunnerException, SerialException {
// Cleanup the serial buffer
try {

View File

@ -0,0 +1,127 @@
package processing.app.forms;
import processing.app.Base;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import static processing.app.I18n._;
public class PasswordAuthorizationDialog extends JDialog {
protected final JButton uploadButton;
protected final JButton cancelButton;
protected final JLabel typePasswordLabel;
protected final JLabel passwordLabel;
protected final JLabel icon;
protected final JPasswordField passwordField;
protected boolean cancelled;
protected String password;
public PasswordAuthorizationDialog(Frame parent) {
super(parent, true);
this.cancelled = false;
this.password = null;
typePasswordLabel = new JLabel();
icon = new JLabel();
passwordLabel = new JLabel();
passwordField = new JPasswordField();
uploadButton = new JButton();
cancelButton = new JButton();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
typePasswordLabel.setText(_("Type board password to upload a new sketch"));
icon.setIcon(new ImageIcon(new File(Base.getContentFile("lib"), "theme/lock.png").getAbsolutePath()));
passwordLabel.setText(_("Password:"));
passwordField.setText("");
passwordField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
uploadButtonPressed(evt);
}
});
uploadButton.setText(_("Upload"));
uploadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
uploadButtonPressed(evt);
}
});
cancelButton.setText(_("Cancel"));
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
cancelButtonPressed(evt);
}
});
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(icon, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(typePasswordLabel)
.addGroup(layout.createSequentialGroup()
.addComponent(passwordLabel)
.addGap(4, 4, 4)
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, 300, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(20, Short.MAX_VALUE))
.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cancelButton)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(uploadButton)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(icon)
.addComponent(typePasswordLabel))
.addGap(5, 5, 5)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(passwordLabel)
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(cancelButton)
.addComponent(uploadButton))
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}
private void cancelButtonPressed(ActionEvent event) {
this.cancelled = true;
this.dispose();
}
public void uploadButtonPressed(ActionEvent event) {
this.password = new String(passwordField.getPassword());
this.dispose();
}
public String getPassword() {
return this.password;
}
public boolean isCancelled() {
return cancelled;
}
}