Ticket #801: josm-utf8-logins.patch
File josm-utf8-logins.patch, 3.9 KB (added by , 16 years ago) |
---|
-
src/org/openstreetmap/josm/tools/Base64.java
1 1 // License: GPL. Copyright 2007 by Immanuel Scholz and others 2 2 package org.openstreetmap.josm.tools; 3 3 4 import java.nio.ByteBuffer; 5 4 6 public class Base64 { 5 7 6 8 private static String enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 7 9 8 9 10 11 12 10 public static String encode(String s) { 11 StringBuilder out = new StringBuilder(); 12 for (int i = 0; i < (s.length()+2)/3; ++i) { 13 int l = Math.min(3, s.length()-i*3); 14 String buf = s.substring(i*3, i*3+l); 13 15 out.append(enc.charAt(buf.charAt(0)>>2)); 14 out.append(enc.charAt((buf.charAt(0) & 0x03) << 4 | (l==1?0:(buf.charAt(1) & 0xf0) >> 4))); 16 out.append(enc.charAt( 17 (buf.charAt(0) & 0x03) << 4 | 18 (l==1? 19 0: 20 (buf.charAt(1) & 0xf0) >> 4))); 15 21 out.append(l>1?enc.charAt((buf.charAt(1) & 0x0f) << 2 | (l==2?0:(buf.charAt(2) & 0xc0) >> 6)):'='); 16 22 out.append(l>2?enc.charAt(buf.charAt(2) & 0x3f):'='); 17 } 18 return out.toString(); 19 } 23 } 24 return out.toString(); 25 } 26 27 public static String encode(ByteBuffer s) { 28 StringBuilder out = new StringBuilder(); 29 // Read 3 bytes at a time. 30 for (int i = 0; i < (s.limit()+2)/3; ++i) { 31 int l = Math.min(3, s.limit()-i*3); 32 int byte0 = s.get() & 0xff; 33 int byte1 = l>1? s.get() & 0xff : 0; 34 int byte2 = l>2? s.get() & 0xff : 0; 35 36 out.append(enc.charAt(byte0>>2)); 37 out.append(enc.charAt( 38 (byte0 & 0x03) << 4 | 39 (l==1? 40 0: 41 (byte1 & 0xf0) >> 4))); 42 out.append(l>1?enc.charAt((byte1 & 0x0f) << 2 | (l==2?0:(byte2 & 0xc0) >> 6)):'='); 43 out.append(l>2?enc.charAt(byte2 & 0x3f):'='); 44 } 45 return out.toString(); 46 } 20 47 } -
src/org/openstreetmap/josm/io/OsmConnection.java
import java.awt.GridBagLayout; 8 8 import java.net.Authenticator; 9 9 import java.net.HttpURLConnection; 10 10 import java.net.PasswordAuthentication; 11 import java.nio.ByteBuffer; 12 import java.nio.CharBuffer; 13 import java.nio.charset.Charset; 14 import java.nio.charset.CharsetEncoder; 15 import java.nio.charset.CharacterCodingException; 11 16 12 17 import javax.swing.JCheckBox; 13 18 import javax.swing.JLabel; … … public class OsmConnection { 128 133 } 129 134 } 130 135 131 protected void addAuth(HttpURLConnection con) { 132 con.addRequestProperty("Authorization", "Basic "+Base64.encode(Main.pref.get("osm-server.username")+":"+Main.pref.get("osm-server.password"))); 136 protected void addAuth(HttpURLConnection con) throws CharacterCodingException { 137 CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder(); 138 String auth = Main.pref.get("osm-server.username") + ":" + Main.pref.get("osm-server.password"); 139 ByteBuffer bytes = encoder.encode(CharBuffer.wrap(auth)); 140 con.addRequestProperty("Authorization", "Basic "+Base64.encode(bytes)); 133 141 } 134 142 }