Skip to content

Commit 078a6f5

Browse files
author
Federico Fissore
committed
updated commons-codec
introduced password authorization dialog actual sketch posting
1 parent 4cbd1cf commit 078a6f5

File tree

7 files changed

+191
-21
lines changed

7 files changed

+191
-21
lines changed

app/lib/commons-codec-1.2.jar

-28.3 KB
Binary file not shown.

app/lib/commons-codec-1.7.jar

254 KB
Binary file not shown.

app/src/processing/app/Preferences.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,13 @@ static public String get(String attribute /*, String defaultValue */) {
829829
*/
830830
}
831831

832+
public static boolean has(String key) {
833+
return table.containsKey(key);
834+
}
835+
836+
public static void remove(String key) {
837+
table.remove(key);
838+
}
832839

833840
static public String getDefault(String attribute) {
834841
return (String) defaults.get(attribute);
@@ -976,5 +983,5 @@ static public PreferencesMap getMap()
976983
{
977984
return new PreferencesMap(table);
978985
}
979-
986+
980987
}

app/src/processing/app/Sketch.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323

2424
package processing.app;
2525

26+
import org.apache.commons.codec.digest.DigestUtils;
2627
import processing.app.debug.*;
2728
import processing.app.debug.Compiler;
29+
import processing.app.forms.PasswordAuthorizationDialog;
2830
import processing.app.helpers.PreferencesMap;
2931
import processing.app.packages.Library;
3032
import processing.app.packages.LibraryList;
@@ -1598,9 +1600,9 @@ public boolean exportApplet(String appletPath, boolean usingProgrammer)
15981600
// }
15991601

16001602
editor.status.progressNotice(_("Uploading..."));
1601-
upload(appletPath, foundName, usingProgrammer);
1603+
boolean success = upload(appletPath, foundName, usingProgrammer);
16021604
editor.status.progressUpdate(100);
1603-
return true;
1605+
return success;
16041606
}
16051607

16061608

@@ -1656,16 +1658,33 @@ protected void size(PreferencesMap prefs) throws RunnerException {
16561658
System.out.println(_("Low memory available, stability problems may occur"));
16571659
}
16581660

1659-
protected String upload(String buildPath, String suggestedClassName, boolean usingProgrammer) throws RunnerException, SerialException {
1661+
protected boolean upload(String buildPath, String suggestedClassName, boolean usingProgrammer) throws RunnerException, SerialException {
16601662

16611663
TargetPlatform target = Base.getTargetPlatform();
16621664
String board = Preferences.get("board");
16631665

16641666
Uploader uploader = new UploaderFactory().newUploader(target.getBoards().get(board), Preferences.get("serial.port"));
16651667

1668+
if (uploader.requiresAuthorization() && !Preferences.has(uploader.getAuthorizationKey())) {
1669+
PasswordAuthorizationDialog dialog = new PasswordAuthorizationDialog(editor);
1670+
dialog.setLocationRelativeTo(editor);
1671+
dialog.setVisible(true);
1672+
1673+
if (dialog.isCancelled()) {
1674+
editor.statusNotice(_("Upload cancelled"));
1675+
return false;
1676+
}
1677+
1678+
Preferences.set(uploader.getAuthorizationKey(), DigestUtils.sha512Hex(dialog.getPassword()));
1679+
}
1680+
16661681
boolean success = uploader.uploadUsingPreferences(buildPath, suggestedClassName, usingProgrammer);
16671682

1668-
return success ? suggestedClassName : null;
1683+
if (uploader.requiresAuthorization() && !success) {
1684+
Preferences.remove(uploader.getAuthorizationKey());
1685+
}
1686+
1687+
return success;
16691688
}
16701689

16711690

app/src/processing/app/debug/HttpUploader.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.apache.commons.httpclient.methods.multipart.FilePart;
77
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
88
import org.apache.commons.httpclient.methods.multipart.Part;
9+
import processing.app.Preferences;
910
import processing.app.SerialException;
1011

1112
import java.io.File;
@@ -32,6 +33,14 @@ public HttpUploader(String port) {
3233
this.ipAddress = matcher.group(1);
3334
}
3435

36+
public boolean requiresAuthorization() {
37+
return true;
38+
}
39+
40+
public String getAuthorizationKey() {
41+
return "pwd." + ipAddress;
42+
}
43+
3544
@Override
3645
public boolean uploadUsingPreferences(String buildPath, String className, boolean usingProgrammer) throws RunnerException, SerialException {
3746
if (usingProgrammer) {
@@ -41,13 +50,14 @@ public boolean uploadUsingPreferences(String buildPath, String className, boolea
4150

4251
FilePart filePart;
4352
try {
44-
filePart = new FilePart("sketch.hex", new File(buildPath, className + ".hex"));
53+
filePart = new FilePart("sketch", new File(buildPath, className + ".hex"));
4554
} catch (FileNotFoundException e) {
4655
throw new RunnerException(e);
4756
}
4857

4958
Part[] parts = {filePart};
5059
PostMethod post = newPostMethod();
60+
post.setRequestHeader("Cookie", "pwd=" + Preferences.get(getAuthorizationKey()));
5161
post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
5262

5363
int statusCode;
@@ -58,7 +68,6 @@ public boolean uploadUsingPreferences(String buildPath, String className, boolea
5868
}
5969

6070
if (statusCode == HttpStatus.SC_OK) {
61-
System.out.println(_("Sketch uploaded"));
6271
return true;
6372
}
6473

@@ -71,7 +80,7 @@ public boolean uploadUsingPreferences(String buildPath, String className, boolea
7180
}
7281

7382
protected PostMethod newPostMethod() {
74-
return new PostMethod("http://" + ipAddress + ":8000/upload");
83+
return new PostMethod("http://" + ipAddress + ":6571/upload");
7584
}
7685

7786
@Override

app/src/processing/app/debug/Uploader.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,22 @@ public abstract class Uploader implements MessageConsumer {
4848

4949
static InputStream serialInput;
5050
static OutputStream serialOutput;
51-
51+
5252
boolean verbose;
5353

5454
public abstract boolean uploadUsingPreferences(String buildPath, String className, boolean usingProgrammer)
5555
throws RunnerException, SerialException;
56-
56+
5757
public abstract boolean burnBootloader() throws RunnerException;
58-
58+
59+
public boolean requiresAuthorization() {
60+
return false;
61+
}
62+
63+
public String getAuthorizationKey() {
64+
return null;
65+
}
66+
5967
protected void flushSerialBuffer() throws RunnerException, SerialException {
6068
// Cleanup the serial buffer
6169
try {
@@ -76,7 +84,7 @@ protected void flushSerialBuffer() throws RunnerException, SerialException {
7684

7785
serialPort.setDTR(true);
7886
serialPort.setRTS(true);
79-
87+
8088
serialPort.dispose();
8189
} catch (SerialNotFoundException e) {
8290
throw e;
@@ -93,14 +101,14 @@ protected boolean executeUploadCommand(Collection<String> commandDownloader)
93101
return executeUploadCommand(commandArray);
94102
}
95103

96-
protected boolean executeUploadCommand(String commandArray[])
104+
protected boolean executeUploadCommand(String commandArray[])
97105
throws RunnerException
98106
{
99107
firstErrorFound = false; // haven't found any errors yet
100108
secondErrorFound = false;
101109
notFoundError = false;
102110
int result=0; // pre-initialized to quiet a bogus warning from jikes
103-
111+
104112
try {
105113
if (verbose || Preferences.getBoolean("upload.verbose")) {
106114
for(int i = 0; i < commandArray.length; i++) {
@@ -122,10 +130,10 @@ protected boolean executeUploadCommand(String commandArray[])
122130
compiling = false;
123131
} catch (InterruptedException intExc) {
124132
}
125-
}
133+
}
126134
if(exception!=null) {
127135
exception.hideStackTrace();
128-
throw exception;
136+
throw exception;
129137
}
130138
if(result!=0)
131139
return false;
@@ -171,16 +179,16 @@ protected boolean executeUploadCommand(String commandArray[])
171179

172180
public void message(String s) {
173181
// selectively suppress a bunch of avrdude output for AVR109/Caterina that should already be quelled but isn't
174-
if (!Preferences.getBoolean("upload.verbose") && (
182+
if (!Preferences.getBoolean("upload.verbose") && (
175183
s.indexOf("Connecting to programmer:") != -1 ||
176184
s.indexOf("Found programmer: Id = \"CATERIN\"; type = S") != -1 ||
177185
s.indexOf("Software Version = 1.0; No Hardware Version given.") != -1 ||
178186
s.indexOf("Programmer supports auto addr increment.") != -1 ||
179-
s.indexOf("Programmer supports buffered memory access with buffersize=128 bytes.") != -1 ||
180-
s.indexOf("Programmer supports the following devices:") != -1 ||
187+
s.indexOf("Programmer supports buffered memory access with buffersize=128 bytes.") != -1 ||
188+
s.indexOf("Programmer supports the following devices:") != -1 ||
181189
s.indexOf("Device code: 0x44") != -1))
182-
s = "";
183-
190+
s = "";
191+
184192
System.err.print(s);
185193

186194
// ignore cautions
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package processing.app.forms;
2+
3+
import processing.app.Base;
4+
5+
import javax.swing.*;
6+
import java.awt.*;
7+
import java.awt.event.ActionEvent;
8+
import java.awt.event.ActionListener;
9+
import java.io.File;
10+
11+
import static processing.app.I18n._;
12+
13+
public class PasswordAuthorizationDialog extends JDialog {
14+
15+
protected final JButton uploadButton;
16+
protected final JButton cancelButton;
17+
protected final JLabel typePasswordLabel;
18+
protected final JLabel passwordLabel;
19+
protected final JLabel icon;
20+
protected final JPasswordField passwordField;
21+
22+
protected boolean cancelled;
23+
protected String password;
24+
25+
public PasswordAuthorizationDialog(Frame parent) {
26+
super(parent, true);
27+
28+
this.cancelled = false;
29+
this.password = null;
30+
31+
typePasswordLabel = new JLabel();
32+
icon = new JLabel();
33+
passwordLabel = new JLabel();
34+
passwordField = new JPasswordField();
35+
uploadButton = new JButton();
36+
cancelButton = new JButton();
37+
38+
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
39+
40+
typePasswordLabel.setText(_("Type board password to upload a new sketch"));
41+
42+
icon.setIcon(new ImageIcon(new File(Base.getContentFile("lib"), "theme/lock.png").getAbsolutePath()));
43+
44+
passwordLabel.setText(_("Password:"));
45+
46+
passwordField.setText("");
47+
passwordField.addActionListener(new ActionListener() {
48+
public void actionPerformed(ActionEvent evt) {
49+
uploadButtonPressed(evt);
50+
}
51+
});
52+
53+
uploadButton.setText(_("Upload"));
54+
uploadButton.addActionListener(new ActionListener() {
55+
public void actionPerformed(ActionEvent evt) {
56+
uploadButtonPressed(evt);
57+
}
58+
});
59+
60+
cancelButton.setText(_("Cancel"));
61+
cancelButton.addActionListener(new ActionListener() {
62+
public void actionPerformed(ActionEvent evt) {
63+
cancelButtonPressed(evt);
64+
}
65+
});
66+
67+
GroupLayout layout = new GroupLayout(getContentPane());
68+
getContentPane().setLayout(layout);
69+
layout.setHorizontalGroup(
70+
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
71+
.addGroup(layout.createSequentialGroup()
72+
.addContainerGap()
73+
.addComponent(icon, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
74+
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
75+
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
76+
.addComponent(typePasswordLabel)
77+
.addGroup(layout.createSequentialGroup()
78+
.addComponent(passwordLabel)
79+
.addGap(4, 4, 4)
80+
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, 300, GroupLayout.PREFERRED_SIZE)))
81+
.addContainerGap(20, Short.MAX_VALUE))
82+
.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
83+
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
84+
.addComponent(cancelButton)
85+
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
86+
.addComponent(uploadButton)
87+
.addContainerGap())
88+
);
89+
layout.setVerticalGroup(
90+
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
91+
.addGroup(layout.createSequentialGroup()
92+
.addContainerGap()
93+
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
94+
.addComponent(icon)
95+
.addComponent(typePasswordLabel))
96+
.addGap(5, 5, 5)
97+
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
98+
.addComponent(passwordLabel)
99+
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
100+
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
101+
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
102+
.addComponent(cancelButton)
103+
.addComponent(uploadButton))
104+
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
105+
);
106+
107+
pack();
108+
}
109+
110+
private void cancelButtonPressed(ActionEvent event) {
111+
this.cancelled = true;
112+
this.dispose();
113+
}
114+
115+
public void uploadButtonPressed(ActionEvent event) {
116+
this.password = new String(passwordField.getPassword());
117+
this.dispose();
118+
}
119+
120+
public String getPassword() {
121+
return this.password;
122+
}
123+
124+
public boolean isCancelled() {
125+
return cancelled;
126+
}
127+
}

0 commit comments

Comments
 (0)