Define our own version of a URL encoding helper

We may not always depend upon the org.mortbay.util package.  The
code to implement a Map<String,String> that converts into a URL
is quite simple given the stock java.net.URLEncoder, so we are
better off using that for our contact store client.

Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce
2009-03-10 15:04:18 -07:00
parent b4325a3a85
commit 0e19c64d0f
2 changed files with 70 additions and 7 deletions

View File

@@ -36,7 +36,6 @@ import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPUtil;
import org.mortbay.util.UrlEncoded;
import org.spearce.jgit.util.NB;
import java.io.ByteArrayOutputStream;
@@ -161,17 +160,17 @@ public class EncryptedContactStore {
final Timestamp filedOn = account.getContactFiledOn();
final UrlEncoded u = new UrlEncoded();
if (storeAPPSEC != null) {
u.add("APPSEC", storeAPPSEC);
u.put("APPSEC", storeAPPSEC);
}
if (account.getPreferredEmail() != null) {
u.add("email", account.getPreferredEmail());
u.put("email", account.getPreferredEmail());
}
if (filedOn != null) {
u.add("filed", String.valueOf(filedOn.getTime() / 1000L));
u.put("filed", String.valueOf(filedOn.getTime() / 1000L));
}
u.add("account_id", String.valueOf(account.getId().get()));
u.add("data", encStr);
final byte[] body = u.encode().getBytes("UTF-8");
u.put("account_id", String.valueOf(account.getId().get()));
u.put("data", encStr);
final byte[] body = u.toString().getBytes("UTF-8");
final HttpURLConnection c = (HttpURLConnection) storeUrl.openConnection();
c.setRequestMethod("POST");

View File

@@ -0,0 +1,64 @@
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;
public class UrlEncoded extends LinkedHashMap<String, String> {
private String url;
public UrlEncoded() {
}
public UrlEncoded(final String url) {
this.url = url;
}
@Override
public String toString() {
final StringBuilder buffer = new StringBuilder();
char separator = 0;
if (url != null) {
separator = '?';
buffer.append(url);
}
for (final Map.Entry<String, String> entry : entrySet()) {
final String key = entry.getKey();
final String val = entry.getValue();
if (separator != 0) {
buffer.append(separator);
}
buffer.append(encode(key));
buffer.append('=');
if (val != null) {
buffer.append(encode(val));
}
separator = '&';
}
return buffer.toString();
}
private static String encode(final String str) {
try {
return URLEncoder.encode(str, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}