diff --git a/OSVUploadr/pom.xml b/OSVUploadr/pom.xml index 90988d0..8df8cf2 100644 --- a/OSVUploadr/pom.xml +++ b/OSVUploadr/pom.xml @@ -3,7 +3,7 @@ 4.0.0 ca.osmcanada OSVUploadr - 0.1.2.1.1-ALPHA + 0.1.2.2-ALPHA jar @@ -16,6 +16,11 @@ metadata-extractor 2.9.1 + + org.apache.httpcomponents + httpclient + 4.5.2 + UTF-8 diff --git a/OSVUploadr/pom.xml.orig b/OSVUploadr/pom.xml.orig new file mode 100644 index 0000000..2a76ce3 --- /dev/null +++ b/OSVUploadr/pom.xml.orig @@ -0,0 +1,62 @@ + + + 4.0.0 + ca.osmcanada + OSVUploadr +<<<<<<< HEAD + 0.1.2.1.1-ALPHA +======= + 0.1.2.2-ALPHA +>>>>>>> Automatic oAuth + jar + + + com.github.scribejava + scribejava-apis + 2.8.1 + + + com.drewnoakes + metadata-extractor + 2.9.1 + + + org.apache.httpcomponents + httpclient + 4.5.2 + + + + UTF-8 + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-assembly-plugin + 2.6 + + + + ca.osmcanada.osvuploadr.JFMain + + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + + \ No newline at end of file diff --git a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/API/OSMApi.java b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/API/OSMApi.java index 3320e9e..d5a992e 100644 --- a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/API/OSMApi.java +++ b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/API/OSMApi.java @@ -11,7 +11,7 @@ * @author Jamie Nadeau */ public class OSMApi extends DefaultApi10a { - private static final String AUTHORIZE_URL = "http://www.openstreetmap.org/oauth/authorize?oauth_token=%s"; + private static final String AUTHORIZE_URL = "https://www.openstreetmap.org/oauth/authorize?oauth_token=%s"; private static final String REQUEST_TOKEN_RESOURCE = "www.openstreetmap.org/oauth/request_token"; private static final String ACCESS_TOKEN_RESOURCE = "www.openstreetmap.org/oauth/access_token"; diff --git a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/FolderCleaner.java b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/FolderCleaner.java index 05721b4..675e859 100644 --- a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/FolderCleaner.java +++ b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/FolderCleaner.java @@ -32,6 +32,7 @@ public class FolderCleaner { int dist_threshold = 4; // Minimum distance a photo should move to not be considered a duplicate (in meters) int radius_threshold = 20; // Minimum turn radius a photo should move to not be considered a duplicate (degrees) String duplicate_folder="duplicates"; + JFMain info=null; private double calc_distance(double lon1, double lat1, double lon2, double lat2){ //haversine formula @@ -85,6 +86,9 @@ public int compare(File f1, File f2) }}); for(File f:file_list){ + if(info!=null){ + info.SetInfoBoxText(f.getPath()); + } ImageProperties imp = Helper.getImageProperties(f); if(is_first){ is_first=false; @@ -151,10 +155,18 @@ public void RemoveDuplicates(){ } } do_science(); + if(info!=null){ + info.SetInfoBoxText("Done"); + } + } + + public void setInfoBox(JFMain frame){ + info=frame; } public FolderCleaner(String Folder){ _folder=Folder; } + } diff --git a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JFMain.java b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JFMain.java index 799f3ee..c1fb2db 100644 --- a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JFMain.java +++ b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JFMain.java @@ -12,7 +12,7 @@ * @author Jamie Nadeau */ public class JFMain extends javax.swing.JFrame { - + static JPInfoBox jib = new JPInfoBox(); /** * Creates new form JFMain */ @@ -22,7 +22,20 @@ public JFMain() { this.setSize(640,320); this.setTitle("OSVUploadr"); } - + + public void ShowInfoBox(){ + jib.setVisible(true); + this.setSize(640,470); + this.pack(); + } + public void HideInfoBox(){ + jib.setVisible(false); + this.setSize(640,320); + } + + public void SetInfoBoxText(String str){ + jib.SetProcessingText(str); + } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always @@ -81,7 +94,9 @@ public void run() { JFMain jf =new JFMain(); JPMain jp = new JPMain(); jp.setVisible(true); - jf.add(jp,BorderLayout.CENTER); + jib.setVisible(false); + jf.add(jp,BorderLayout.NORTH); + jf.add(jib,BorderLayout.SOUTH); jf.setVisible(true); } }); diff --git a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPInfoBox.form b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPInfoBox.form new file mode 100644 index 0000000..348d4e1 --- /dev/null +++ b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPInfoBox.form @@ -0,0 +1,55 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPInfoBox.java b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPInfoBox.java new file mode 100644 index 0000000..0f1ac45 --- /dev/null +++ b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPInfoBox.java @@ -0,0 +1,73 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package ca.osmcanada.osvuploadr; + +import javax.swing.UIManager; + +/** + * + * @author Nadeaj + */ +public class JPInfoBox extends javax.swing.JPanel { + + /** + * Creates new form JPInfoBox + */ + public JPInfoBox() { + try{ + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } + catch(Exception ex){} + initComponents(); + } + + public void SetProcessingText(String str){ + jlProcessing.setText(str); + } + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + jLabel1 = new javax.swing.JLabel(); + jlProcessing = new javax.swing.JLabel(); + + setPreferredSize(new java.awt.Dimension(623, 150)); + + jLabel1.setText("Currently Processing:"); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); + this.setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jLabel1) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jlProcessing, javax.swing.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel1) + .addComponent(jlProcessing, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addContainerGap(78, Short.MAX_VALUE)) + ); + }// //GEN-END:initComponents + + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JLabel jLabel1; + private javax.swing.JLabel jlProcessing; + // End of variables declaration//GEN-END:variables +} diff --git a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.form b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.form index f8418b0..daddf51 100644 --- a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.form +++ b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.form @@ -26,16 +26,16 @@ - - + + - - - + + + @@ -53,16 +53,16 @@ - - + + - + - + - + - + @@ -71,7 +71,7 @@ - + @@ -85,7 +85,7 @@ - + @@ -101,15 +101,15 @@
- + - + - + @@ -120,10 +120,10 @@ - + - + @@ -134,15 +134,15 @@ - + - + - + diff --git a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.java b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.java index 9677e8a..382d7ed 100644 --- a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.java +++ b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.java @@ -30,7 +30,6 @@ import java.util.Comparator; import java.util.List; import ca.osmcanada.osvuploadr.struct.ImageProperties; -import com.drew.metadata.exif.ExifSubIFDDirectory; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.InputStream; @@ -49,7 +48,17 @@ import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import ca.osmcanada.osvuploadr.Utils.*; +import ca.osmcanada.osvuploadr.struct.PageContent; +import java.nio.charset.Charset; +import org.apache.http.client.HttpClient; +import org.apache.http.client.CookieStore; +import org.apache.http.cookie.Cookie; +import org.apache.http.impl.client.BasicCookieStore; +import org.apache.http.impl.client.HttpClientBuilder; + /** * @@ -57,11 +66,13 @@ */ public class JPMain extends javax.swing.JPanel { - private final String BASE_URL="http://www.openstreetmap.org/"; + private final String BASE_URL="https://www.openstreetmap.org/"; private final String URL_SEQUENCE = "http://openstreetview.com/1.0/sequence/"; private final String URL_PHOTO = "http://openstreetview.com/1.0/photo/"; private final String URL_FINISH = "http://openstreetview.com/1.0/sequence/finished-uploading/"; private final String URL_ACCESS = "http://openstreetview.com/auth/openstreetmap/client_auth"; + private final String API_KEY = "rBWV8Eaottv44tXfdLofdNvVemHOL62Lsutpb9tw"; + private final String API_SECRET = "rpmeZIp49sEjjcz91X9dsY0vD1PpEduixuPy8T6S"; private String last_dir =""; UploadManager um; @@ -76,10 +87,121 @@ public JPMain() { initComponents(); } + public String GetOSMUser(String usr, String psw)throws IOException{ + final OAuth10aService service = new ServiceBuilder() + .apiKey(API_KEY) + .apiSecret(API_SECRET) + .build(OSMApi.instance()); + final OAuth1RequestToken requestToken = service.getRequestToken(); + String url = service.getAuthorizationUrl(requestToken); + + //Automated grant and login*************************************** + //Get logon form + CookieStore httpCookieStore = new BasicCookieStore(); + HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(httpCookieStore).build(); + //post.setHeader("User-Agent", USER_AGENT); + try{ + PageContent pc = Helper.GetPageContent(url,client); + + List cookies = httpCookieStore.getCookies(); + System.out.println("Getting OSM Login page"); + String page = pc.GetPage(); + + System.out.println("Sending username and password"); + + int start_indx = page.indexOf("value=\"",page.indexOf("name=\"authenticity_token\""))+7; + int end_indx = page.indexOf("\"",start_indx); + + String utf8 = "✓"; + String authenticity_token = page.substring(start_indx,end_indx); + + start_indx = page.indexOf("value=\"",page.indexOf("name=\"referer\""))+7; + end_indx = page.indexOf("\"",start_indx); + String referer = page.substring(start_indx,end_indx); + + Map arguments = new HashMap<>(); + arguments.put("utf8", utf8); + arguments.put("authenticity_token", authenticity_token); + arguments.put("referer", referer); + arguments.put("username", usr); + arguments.put("password", psw); + arguments.put("commit", "Login"); + arguments.put("openid_url", ""); + + System.out.println("Logging in"); + page = sendForm(BASE_URL+"login",arguments,"POST",cookies); + + + + //Proccessing grant access page + System.out.println("Processing Granting Access page"); + start_indx = page.indexOf("",start_indx); + String tmp = page.substring(start_indx,end_indx); //Get aproximate location of pin code + + Pattern p = Pattern.compile("([A-Za-z0-9]){15,25}"); + Matcher m = p.matcher(tmp); + String pin = ""; + if(m.find()) + { + pin = m.group(); + } + final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, pin); + return accessToken.getToken()+"|"+accessToken.getTokenSecret(); + + } + catch(Exception ex) + { + Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, null, ex); + } + + + + //End Automated grant and login ********************************** + return ""; + } + public String GetOSMUser() throws IOException{ final OAuth10aService service = new ServiceBuilder() - .apiKey("rBWV8Eaottv44tXfdLofdNvVemHOL62Lsutpb9tw") - .apiSecret("rpmeZIp49sEjjcz91X9dsY0vD1PpEduixuPy8T6S") + .apiKey(API_KEY) + .apiSecret(API_SECRET) .build(OSMApi.instance()); final OAuth1RequestToken requestToken = service.getRequestToken(); @@ -88,21 +210,111 @@ public String GetOSMUser() throws IOException{ //java.awt.Desktop.getDesktop().browse(java.net.URI.create(url)); String pin_code = JOptionPane.showInputDialog(null, "Authorization window has opened, please paste authorization code below once authorized.\nWith out the \".\" at the end", "Authorization Required", JOptionPane.INFORMATION_MESSAGE); final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, pin_code); - final OAuthRequest request = new OAuthRequest(Verb.GET, BASE_URL + "api/0.6/user/details", service); - service.signRequest(accessToken, request); - Response response = request.send(); - String body = response.getBody(); - int indxUserId= body.indexOf("user id=\""); - int indxEndUserId = body.indexOf("\"",indxUserId+9); - int indxUsername= body.indexOf("display_name=\""); - int indxEndUsername = body.indexOf("\"",indxUsername+14); + //final OAuthRequest request = new OAuthRequest(Verb.GET, BASE_URL + "api/0.6/user/details", service); + //service.signRequest(accessToken, request); + //Response response = request.send(); + //String body = response.getBody(); + //int indxUserId= body.indexOf("user id=\""); + //int indxEndUserId = body.indexOf("\"",indxUserId+9); + //int indxUsername= body.indexOf("display_name=\""); + //int indxEndUsername = body.indexOf("\"",indxUsername+14); //String user_id = body.substring(indxUserId+9,indxEndUserId); //String username = body.substring(indxUsername+14,indxEndUsername); //return user_id +";"+username; return accessToken.getToken()+"|"+accessToken.getTokenSecret(); - } + } + + private int FindOpening(String str,int current_index,String OpeningChar){ + for(int i=current_index;i>=0;i--){ + if(str.substring(i,i+1).equals("<")){ + return i; + } + } + return 0; + } + private String sendForm(String target_url,Map arguments,String method,List cookielist){ + try{ + URL url = new URL(target_url); + HttpURLConnection con = (HttpURLConnection)url.openConnection(); + //HttpURLConnection http = (HttpURLConnection)con; + con.setRequestMethod(method); // PUT is another valid option + con.setDoOutput(true); + con.setInstanceFollowRedirects(false); + String cookiestr=""; + if(cookielist!=null){ + if(cookielist.size()>0){ + for(Cookie cookie:cookielist){ + if(!cookiestr.equals("")){ + cookiestr+=";" + cookie.getName() +"=" +cookie.getValue(); + } + else{ + cookiestr+=cookie.getName() +"=" +cookie.getValue(); + } + } + con.setRequestProperty("Cookie", cookiestr); + } + } + + con.setReadTimeout(5000); + + StringJoiner sj = new StringJoiner("&"); + for(Map.Entry entry : arguments.entrySet()) + sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "=" + URLEncoder.encode(entry.getValue(), "UTF-8")); + byte[] out = sj.toString().getBytes(StandardCharsets.UTF_8); + int length = out.length; + + con.setFixedLengthStreamingMode(length); + con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); + con.setRequestProperty("Accept-Language", "en-us;"); + con.connect(); + try(OutputStream os = con.getOutputStream()) { + os.write(out); + os.close(); + } + + boolean redirect = false; + int status = con.getResponseCode(); + + if(status != HttpURLConnection.HTTP_OK) { + if (status == HttpURLConnection.HTTP_MOVED_TEMP + || status == HttpURLConnection.HTTP_MOVED_PERM + || status == HttpURLConnection.HTTP_SEE_OTHER) + redirect = true; + } + + if(redirect){ + String newURL = con.getHeaderField("Location"); + String cookies = con.getHeaderField("Set-Cookie"); + if(cookies == null){ + cookies= cookiestr; + } + con = (HttpURLConnection) new URL(newURL).openConnection(); + con.setRequestProperty("Cookie", cookies); + } + + InputStream is = con.getInputStream(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + byte buf[] = new byte[1024]; + int letti; + + while ((letti = is.read(buf)) > 0) + baos.write(buf, 0, letti); + + String data = new String(baos.toByteArray(),Charset.forName("UTF-8")); + con.disconnect(); + + return data; + + } + catch(Exception ex){ + Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, null, ex); + } + return ""; + } + private void sendFile(OutputStream out, String name, InputStream in, String fileName) { try { String o = "Content-Disposition: form-data; name=\"" + URLEncoder.encode(name,"UTF-8") + "\"; filename=\"" + URLEncoder.encode(fileName,"UTF-8") + "\"\r\nContent-Type: image/jpeg\r\n\r\n"; @@ -510,21 +722,21 @@ public int compare(File f1, File f2) // //GEN-BEGIN:initComponents private void initComponents() { - jButton1 = new javax.swing.JButton(); + jbAdd = new javax.swing.JButton(); listDir = new java.awt.List(); jLabel1 = new javax.swing.JLabel(); - jButton2 = new javax.swing.JButton(); - jButton3 = new javax.swing.JButton(); - jButton4 = new javax.swing.JButton(); - jButton5 = new javax.swing.JButton(); - - jButton1.setText("Add Folder"); - jButton1.setMaximumSize(new java.awt.Dimension(123, 23)); - jButton1.setMinimumSize(new java.awt.Dimension(123, 23)); - jButton1.setPreferredSize(new java.awt.Dimension(123, 23)); - jButton1.addActionListener(new java.awt.event.ActionListener() { + jbRemoveDup = new javax.swing.JButton(); + jbUpload = new javax.swing.JButton(); + jbExit = new javax.swing.JButton(); + jbRemove = new javax.swing.JButton(); + + jbAdd.setText("Add Folder"); + jbAdd.setMaximumSize(new java.awt.Dimension(123, 23)); + jbAdd.setMinimumSize(new java.awt.Dimension(123, 23)); + jbAdd.setPreferredSize(new java.awt.Dimension(123, 23)); + jbAdd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton1ActionPerformed(evt); + jbAddActionPerformed(evt); } }); @@ -537,35 +749,35 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { jLabel1.setText("Directories"); - jButton2.setText("Remove Duplicates"); - jButton2.addActionListener(new java.awt.event.ActionListener() { + jbRemoveDup.setText("Remove Duplicates"); + jbRemoveDup.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton2ActionPerformed(evt); + jbRemoveDupActionPerformed(evt); } }); - jButton3.setText("Upload"); - jButton3.setMaximumSize(new java.awt.Dimension(123, 23)); - jButton3.setMinimumSize(new java.awt.Dimension(123, 23)); - jButton3.addActionListener(new java.awt.event.ActionListener() { + jbUpload.setText("Upload"); + jbUpload.setMaximumSize(new java.awt.Dimension(123, 23)); + jbUpload.setMinimumSize(new java.awt.Dimension(123, 23)); + jbUpload.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton3ActionPerformed(evt); + jbUploadActionPerformed(evt); } }); - jButton4.setText("Exit"); - jButton4.setMaximumSize(new java.awt.Dimension(123, 23)); - jButton4.setMinimumSize(new java.awt.Dimension(123, 23)); - jButton4.addActionListener(new java.awt.event.ActionListener() { + jbExit.setText("Exit"); + jbExit.setMaximumSize(new java.awt.Dimension(123, 23)); + jbExit.setMinimumSize(new java.awt.Dimension(123, 23)); + jbExit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton4ActionPerformed(evt); + jbExitActionPerformed(evt); } }); - jButton5.setText("Remove Folder"); - jButton5.addActionListener(new java.awt.event.ActionListener() { + jbRemove.setText("Remove Folder"); + jbRemove.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton5ActionPerformed(evt); + jbRemoveActionPerformed(evt); } }); @@ -583,14 +795,14 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { .addGroup(layout.createSequentialGroup() .addGap(24, 24, 24) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) + .addComponent(jbAdd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jbRemove, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING) - .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(jButton4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))))) + .addComponent(jbRemoveDup, javax.swing.GroupLayout.Alignment.TRAILING) + .addComponent(jbUpload, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jbExit, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( @@ -602,15 +814,15 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(listDir, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() - .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jbAdd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jButton5) + .addComponent(jbRemove) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jButton2) + .addComponent(jbRemoveDup) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jbUpload, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) + .addComponent(jbExit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(41, Short.MAX_VALUE)) ); }// //GEN-END:initComponents @@ -619,12 +831,12 @@ private void listDirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST // TODO add your handling code here: }//GEN-LAST:event_listDirActionPerformed - private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed + private void jbExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbExitActionPerformed // TODO add your handling code here: System.exit(0); - }//GEN-LAST:event_jButton4ActionPerformed + }//GEN-LAST:event_jbExitActionPerformed - private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed + private void jbAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAddActionPerformed JFileChooser fc = new JFileChooser(); if(!last_dir.isEmpty()){ fc.setCurrentDirectory(new java.io.File(last_dir)); // start at application current directory @@ -653,9 +865,9 @@ public boolean accept(File current, String name){ } } } - }//GEN-LAST:event_jButton1ActionPerformed + }//GEN-LAST:event_jbAddActionPerformed - private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed + private void jbRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbRemoveActionPerformed int selectedCnt = listDir.getSelectedItems().length; if(selectedCnt > 0) { @@ -664,9 +876,9 @@ private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS listDir.remove(listDir.getSelectedIndexes()[i]); } } - }//GEN-LAST:event_jButton5ActionPerformed + }//GEN-LAST:event_jbRemoveActionPerformed - private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed + private void jbUploadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbUploadActionPerformed String path = ca.osmcanada.osvuploadr.JFMain.class.getProtectionDomain().getCodeSource().getLocation().getPath(); String decodedPath = ""; try{ @@ -682,8 +894,40 @@ private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS if(!id.exists()) { try{ + String[] buttons={"Automatically","Manually","Cancel"}; + int rc = JOptionPane.showOptionDialog(null,"You must login to OSM to continue the upload. Would you like the automated version or the manual version?","Confirmation",JOptionPane.INFORMATION_MESSAGE,0,null,buttons,buttons[0]); + String token=""; System.out.println("GetOSMUser"); - String token = GetOSMUser(); + switch(rc){ + case 0: + String usr = ""; + String psw = ""; + JTextField tf = new JTextField(); + JPasswordField pf = new JPasswordField(); + rc = JOptionPane.showConfirmDialog(null,tf,"Enter email address or OSM username", JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE); + if(rc == JOptionPane.OK_OPTION){ + usr = tf.getText(); + } + else{ + return; + } + + rc = JOptionPane.showConfirmDialog(null,pf,"Please enter password", JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE); + if(rc == JOptionPane.OK_OPTION){ + psw = new String(pf.getPassword()); + } + else{ + return; + } + token = GetOSMUser(usr,psw); + break; + case 1: + token = GetOSMUser(); + break; + case 2: + return; + + } Path targetPath = Paths.get("./access_token.txt"); byte[] bytes = token.split("\\|")[0].getBytes(StandardCharsets.UTF_8); Files.write(targetPath, bytes, StandardOpenOption.CREATE); @@ -719,23 +963,32 @@ private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS //um = new UploadManager(listDir.getItems()); //um.start(); - }//GEN-LAST:event_jButton3ActionPerformed + }//GEN-LAST:event_jbUploadActionPerformed - private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed + private void jbRemoveDupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbRemoveDupActionPerformed + JFMain topframe = (JFMain)SwingUtilities.getWindowAncestor(this); + topframe.ShowInfoBox(); + topframe.SetInfoBoxText("test"); for(String item:listDir.getItems()){ - FolderCleaner fc = new FolderCleaner(item); - fc.RemoveDuplicates(); - } - }//GEN-LAST:event_jButton2ActionPerformed + Thread t = new Thread(){ + public void run(){ + FolderCleaner fc = new FolderCleaner(item); + fc.setInfoBox(topframe); + fc.RemoveDuplicates(); + } + }; + t.start(); + } + }//GEN-LAST:event_jbRemoveDupActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables - private javax.swing.JButton jButton1; - private javax.swing.JButton jButton2; - private javax.swing.JButton jButton3; - private javax.swing.JButton jButton4; - private javax.swing.JButton jButton5; private javax.swing.JLabel jLabel1; + private javax.swing.JButton jbAdd; + private javax.swing.JButton jbExit; + private javax.swing.JButton jbRemove; + private javax.swing.JButton jbRemoveDup; + private javax.swing.JButton jbUpload; private java.awt.List listDir; // End of variables declaration//GEN-END:variables } diff --git a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/Utils/Helper.java b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/Utils/Helper.java index a3da993..71ae4bd 100644 --- a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/Utils/Helper.java +++ b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/Utils/Helper.java @@ -7,11 +7,13 @@ import ca.osmcanada.osvuploadr.JPMain; import ca.osmcanada.osvuploadr.struct.ImageProperties; +import ca.osmcanada.osvuploadr.struct.PageContent; import com.drew.imaging.ImageMetadataReader; import com.drew.metadata.Metadata; import com.drew.metadata.exif.ExifSubIFDDirectory; import com.drew.metadata.exif.GpsDirectory; import java.awt.Desktop; +import java.io.BufferedReader; import java.io.File; import java.net.URI; import java.util.Date; @@ -20,12 +22,20 @@ import java.util.ArrayList; import java.util.List; import java.io.IOException; +import java.io.InputStreamReader; +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.HttpClientBuilder; /** * * @author Jamie Nadeau */ public final class Helper { + private final static String USER_AGENT="Mozilla/5.0"; public static enum EnumOS { linux, macos, solaris, unknown, windows; @@ -212,5 +222,42 @@ public static EnumOS getOs() { } private static void logOut(String msg) { System.out.println(msg); - } + } + + public static PageContent GetPageContent(String url, HttpClient client) throws Exception{ + PageContent pc = new PageContent(); + + HttpGet request = new HttpGet(url); + request.setHeader("User-Agent", USER_AGENT); + request.setHeader("Accept", + "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); + request.setHeader("Accept-Language", "en-US,en;q=0.5"); + + HttpResponse response = client.execute(request); + int responseCode = response.getStatusLine().getStatusCode(); + System.out.println("\nSending 'GET' request to URL : " + url); + System.out.println("Response Code : " + responseCode); + + BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); + + StringBuffer result = new StringBuffer(); + String line = ""; + while ((line = rd.readLine()) != null) { + result.append(line); + } + //Setting cookies + pc.SetCookies(response.getFirstHeader("Set-Cookie") == null ? "" :response.getFirstHeader("Set-Cookie").toString()); + pc.SetPage(result.toString()); + + return pc; + } + + public static List getFormParams(String html,List FieldList) + { + List fields = new ArrayList(); + for(String field:FieldList){ + //TODO: Get fields from HTML + } + return null; + } } diff --git a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/struct/PageContent.java b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/struct/PageContent.java new file mode 100644 index 0000000..305822b --- /dev/null +++ b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/struct/PageContent.java @@ -0,0 +1,28 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package ca.osmcanada.osvuploadr.struct; + +/** + * + * @author Jamie Nadeau + */ +public class PageContent { + String _page; + String _cookies; + + public void SetPage(String Page){ + _page = Page; + } + public void SetCookies(String Cookies){ + _cookies = Cookies; + } + public String GetPage(){ + return _page; + } + public String GetCookies(){ + return _cookies; + } +}