Apply Base64 refactoring to all classes

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@2248 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2014-03-10 22:05:11 +00:00
parent 33c80402b6
commit 0fc908b1e9
7 changed files with 62 additions and 39 deletions

View File

@ -21,7 +21,6 @@ package davmail;
import davmail.exception.DavMailException;
import davmail.exchange.ExchangeSession;
import davmail.ui.tray.DavGatewayTray;
import org.apache.commons.codec.binary.Base64;
import java.io.*;
import java.net.Socket;
@ -267,12 +266,4 @@ public class AbstractConnection extends Thread {
DavGatewayTray.warn(new BundleMessage("LOG_EXCEPTION_CLOSING_CLIENT_SOCKET"), e2);
}
}
protected String base64Encode(String value) throws UnsupportedEncodingException {
return new String(Base64.encodeBase64(value.getBytes("UTF-8")), "UTF-8");
}
protected String base64Decode(String value) throws UnsupportedEncodingException {
return new String(Base64.decodeBase64(value.getBytes("UTF-8")), "UTF-8");
}
}

View File

@ -472,7 +472,7 @@ public class CaldavConnection extends AbstractConnection {
}
if (request.hasProperty("getctag")) {
response.appendProperty("CS:getctag", "CS=\"http://calendarserver.org/ns/\"",
IOUtil.encodeBase64(folder.ctag));
IOUtil.encodeBase64AsString(folder.ctag));
}
if (request.hasProperty("displayname")) {
if (subFolder == null || subFolder.length() == 0) {
@ -521,8 +521,8 @@ public class CaldavConnection extends AbstractConnection {
if (!session.isSharedFolder(folderPath)) {
try {
ExchangeSession.Folder folder = session.getFolder(folderPath);
ctag = IOUtil.encodeBase64(folder.ctag);
etag = IOUtil.encodeBase64(folder.etag);
ctag = IOUtil.encodeBase64AsString(folder.ctag);
etag = IOUtil.encodeBase64AsString(folder.etag);
} catch (HttpException e) {
// unauthorized access, probably an inbox on shared calendar
DavGatewayTray.debug(new BundleMessage("LOG_ACCESS_FORBIDDEN", folderPath, e.getMessage()));
@ -834,7 +834,7 @@ public class CaldavConnection extends AbstractConnection {
if (request.hasProperty("getctag")) {
ExchangeSession.Folder rootFolder = session.getFolder("");
response.appendProperty("CS:getctag", "CS=\"http://calendarserver.org/ns/\"",
IOUtil.encodeBase64(rootFolder.ctag));
IOUtil.encodeBase64AsString(rootFolder.ctag));
}
response.endPropStatOK();
if (request.getDepth() == 1) {

View File

@ -1238,7 +1238,7 @@ public class DavExchangeSession extends ExchangeSession {
String photo = get("photo");
if (photo != null) {
// need to update photo
byte[] resizedImageBytes = IOUtil.resizeImage(Base64.decodeBase64(photo.getBytes("ASCII")), 90);
byte[] resizedImageBytes = IOUtil.resizeImage(IOUtil.decodeBase64(photo), 90);
final PutMethod putmethod = new PutMethod(contactPictureUrl);
putmethod.setRequestHeader("Overwrite", "t");
@ -1342,8 +1342,7 @@ public class DavExchangeSession extends ExchangeSession {
// PropFind PR_INTERNET_CONTENT
String propertyValue = getItemProperty(permanentUrl, "internetContent");
if (propertyValue != null) {
byte[] byteArray = Base64.decodeBase64(propertyValue.getBytes("ASCII"));
result = getICS(new ByteArrayInputStream(byteArray));
result = getICS(new ByteArrayInputStream(IOUtil.decodeBase64(propertyValue)));
}
return result;
}
@ -1706,7 +1705,7 @@ public class DavExchangeSession extends ExchangeSession {
// Set contentclass to make ActiveSync happy
propertyList.add(Field.createDavProperty("contentclass", contentClass));
// ... but also set PR_INTERNET_CONTENT to preserve custom properties
propertyList.add(Field.createDavProperty("internetContent", new String(Base64.encodeBase64(mimeContent))));
propertyList.add(Field.createDavProperty("internetContent",IOUtil.encodeBase64AsString(mimeContent)));
PropPatchMethod propPatchMethod = new PropPatchMethod(encodedHref, propertyList);
int patchStatus = DavGatewayHttpClientFacade.executeHttpMethod(httpClient, propPatchMethod);
if (patchStatus != HttpStatus.SC_MULTI_STATUS) {
@ -2005,8 +2004,8 @@ public class DavExchangeSession extends ExchangeSession {
String base64Property = getPropertyIfExists(properties, alias);
if (base64Property != null) {
try {
property = Base64.decodeBase64(base64Property.getBytes("ASCII"));
} catch (UnsupportedEncodingException e) {
property = IOUtil.decodeBase64(base64Property);
} catch (IOException e) {
LOGGER.warn(e);
}
}
@ -2302,7 +2301,7 @@ public class DavExchangeSession extends ExchangeSession {
while ((length = partInputStream.read(bytes)) > 0) {
baos.write(bytes, 0, length);
}
contactPhoto.content = new String(Base64.encodeBase64(baos.toByteArray()));
contactPhoto.content = IOUtil.encodeBase64AsString(baos.toByteArray());
} finally {
if (inputStream != null) {
try {

View File

@ -44,7 +44,6 @@ import javax.mail.internet.MimeMessage;
import javax.mail.util.SharedByteArrayInputStream;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.SocketException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@ -520,7 +519,7 @@ public class EwsExchangeSession extends ExchangeSession {
throw new IOException(e.getMessage());
}
baos.close();
item.mimeContent = Base64.encodeBase64(baos.toByteArray());
item.mimeContent = IOUtil.encodeBase64(baos.toByteArray());
List<FieldUpdate> fieldUpdates = buildProperties(properties);
if (!properties.containsKey("draft")) {
@ -562,7 +561,7 @@ public class EwsExchangeSession extends ExchangeSession {
protected void sendMessage(String itemClass, byte[] messageBody) throws IOException {
EWSMethod.Item item = new EWSMethod.Item();
item.type = "Message";
item.mimeContent = Base64.encodeBase64(messageBody);
item.mimeContent = IOUtil.encodeBase64(messageBody);
if (itemClass != null) {
item.put("ItemClass", itemClass);
}
@ -774,8 +773,8 @@ public class EwsExchangeSession extends ExchangeSession {
resultCount = results.size();
if (resultCount > 0 && LOGGER.isDebugEnabled()) {
LOGGER.debug("Search items current count: "+resultCount+" fetchCount: "+fetchCount
+" highest uid: "+results.get(0).get(Field.get("imapUid").getResponseName())
+" lowest uid: "+results.get(resultCount-1).get(Field.get("imapUid").getResponseName()));
+" highest uid: "+results.get(0).get(Field.get("imapUid").getResponseName())
+" lowest uid: "+results.get(resultCount-1).get(Field.get("imapUid").getResponseName()));
}
if (Thread.interrupted()) {
LOGGER.debug("Search items failed: Interrupted by client");
@ -876,6 +875,7 @@ public class EwsExchangeSession extends ExchangeSession {
&& ((ExtendedFieldURI) fieldURI).propertyType == ExtendedFieldURI.PropertyType.Integer) {
// check value
try {
//noinspection ResultOfMethodCallIgnored
Integer.parseInt(value);
buffer.append(value);
} catch (NumberFormatException e) {
@ -1399,9 +1399,9 @@ public class EwsExchangeSession extends ExchangeSession {
if (photo != null) {
// convert image to jpeg
byte[] resizedImageBytes = IOUtil.resizeImage(Base64.decodeBase64(photo.getBytes()), 90);
byte[] resizedImageBytes = IOUtil.resizeImage(IOUtil.decodeBase64(photo), 90);
FileAttachment attachment = new FileAttachment("ContactPicture.jpg", "image/jpeg", new String(Base64.encodeBase64(resizedImageBytes)));
FileAttachment attachment = new FileAttachment("ContactPicture.jpg", "image/jpeg", IOUtil.encodeBase64AsString(resizedImageBytes));
attachment.setIsContactPhoto(true);
// update photo attachment
@ -1536,7 +1536,7 @@ public class EwsExchangeSession extends ExchangeSession {
if (currentItemId != null) {
/*Set<FieldUpdate> updates = new HashSet<FieldUpdate>();
// TODO: update properties instead of brute force delete/add
updates.add(new FieldUpdate(Field.get("mimeContent"), new String(Base64.encodeBase64(itemContent))));
updates.add(new FieldUpdate(Field.get("mimeContent"), new String(Base64.encodeBase64AsString(itemContent))));
// update
createOrUpdateItemMethod = new UpdateItemMethod(MessageDisposition.SaveOnly,
ConflictResolution.AutoResolve,
@ -1549,7 +1549,7 @@ public class EwsExchangeSession extends ExchangeSession {
// create
EWSMethod.Item newItem = new EWSMethod.Item();
newItem.type = "CalendarItem";
newItem.mimeContent = Base64.encodeBase64(vCalendar.toString().getBytes("UTF-8"));
newItem.mimeContent = IOUtil.encodeBase64(vCalendar.toString());
ArrayList<FieldUpdate> updates = new ArrayList<FieldUpdate>();
if (!vCalendar.hasVAlarm()) {
updates.add(Field.createFieldUpdate("reminderset", "false"));

View File

@ -126,12 +126,12 @@ public class ImapConnection extends AbstractConnection {
String authenticationMethod = tokens.nextToken();
if ("LOGIN".equalsIgnoreCase(authenticationMethod)) {
try {
sendClient("+ " + IOUtil.encodeBase64("Username:"));
sendClient("+ " + IOUtil.encodeBase64AsString("Username:"));
state = State.LOGIN;
userName = IOUtil.decodeBase64AsString(readClient());
// detect shared mailbox access
splitUserName();
sendClient("+ " + IOUtil.encodeBase64("Password:"));
sendClient("+ " + IOUtil.encodeBase64AsString("Password:"));
state = State.PASSWORD;
password = IOUtil.decodeBase64AsString(readClient());
session = ExchangeSessionFactory.getInstance(userName, password);

View File

@ -25,6 +25,7 @@ import davmail.exception.DavMailException;
import davmail.exchange.DoubleDotInputStream;
import davmail.exchange.ExchangeSessionFactory;
import davmail.ui.tray.DavGatewayTray;
import davmail.util.IOUtil;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
@ -76,12 +77,12 @@ public class SmtpConnection extends AbstractConnection {
if (state == State.LOGIN) {
// AUTH LOGIN, read userName
userName = base64Decode(line);
sendClient("334 " + base64Encode("Password:"));
userName = IOUtil.decodeBase64AsString(line);
sendClient("334 " + IOUtil.encodeBase64AsString("Password:"));
state = State.PASSWORD;
} else if (state == State.PASSWORD) {
// AUTH LOGIN, read password
password = base64Decode(line);
password = IOUtil.decodeBase64AsString(line);
authenticate();
} else if ("QUIT".equalsIgnoreCase(command)) {
sendClient("221 Closing connection");
@ -106,11 +107,11 @@ public class SmtpConnection extends AbstractConnection {
} else if ("LOGIN".equalsIgnoreCase(authType)) {
if (tokens.hasMoreTokens()) {
// user name sent on auth line
userName = base64Decode(tokens.nextToken());
sendClient("334 " + base64Encode("Password:"));
userName = IOUtil.decodeBase64AsString(tokens.nextToken());
sendClient("334 " + IOUtil.encodeBase64AsString("Password:"));
state = State.PASSWORD;
} else {
sendClient("334 " + base64Encode("Username:"));
sendClient("334 " + IOUtil.encodeBase64AsString("Username:"));
state = State.LOGIN;
}
} else {
@ -243,7 +244,7 @@ public class SmtpConnection extends AbstractConnection {
* @throws IOException if invalid credentials
*/
protected void decodeCredentials(String encodedCredentials) throws IOException {
String decodedCredentials = base64Decode(encodedCredentials);
String decodedCredentials = IOUtil.decodeBase64AsString(encodedCredentials);
int startIndex = decodedCredentials.indexOf((char) 0);
if (startIndex >=0) {
int endIndex = decodedCredentials.indexOf((char) 0, startIndex+1);

View File

@ -77,10 +77,42 @@ public final class IOUtil {
* @return base64 value
* @throws IOException on error
*/
public static String encodeBase64(String value) throws IOException {
public static String encodeBase64AsString(String value) throws IOException {
return new String(Base64.encodeBase64(value.getBytes("UTF-8")), "ASCII");
}
/**
* Base64 encode value.
*
* @param value input value
* @return base64 value
* @throws IOException on error
*/
public static String encodeBase64AsString(byte[] value) throws IOException {
return new String(Base64.encodeBase64(value), "ASCII");
}
/**
* Base64 encode value.
*
* @param value input value
* @return base64 value
* @throws IOException on error
*/
public static byte[] encodeBase64(String value) throws IOException {
return Base64.encodeBase64(value.getBytes("UTF-8"));
}
/**
* Base64 encode value.
*
* @param value input value
* @return base64 value
* @throws IOException on error
*/
public static byte[] encodeBase64(byte[] value) throws IOException {
return Base64.encodeBase64(value);
}
/**
* Resize image bytes to a max width or height image size.