use charset in all cases when reading .cvspass

cleanup
This commit is contained in:
Bas Leijdekkers
2011-08-03 22:08:29 +02:00
parent 5893400d50
commit ace95c3b5a
3 changed files with 68 additions and 94 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.*;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.util.SystemProperties;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -28,7 +29,6 @@ import org.jetbrains.annotations.NotNull;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
@@ -60,7 +60,6 @@ public class CvsApplicationLevelConfiguration implements NamedComponent, JDOMExt
@NonNls public static final String DEFAULT = "Default";
public boolean DO_OUTPUT = false;
@NonNls private static final String USER_HOME_PROPERTY = "user.home";
public boolean SEND_ENVIRONMENT_VARIABLES_TO_SERVER = false;
public CvsApplicationLevelConfiguration() {
@@ -79,8 +78,7 @@ public class CvsApplicationLevelConfiguration implements NamedComponent, JDOMExt
public void readExternal(Element element) throws InvalidDataException {
DefaultJDOMExternalizer.readExternal(this, element);
for (Iterator each = element.getChildren(CONFIGURATION_ELEMENT_NAME).iterator(); each.hasNext();) {
Element child = (Element)each.next();
for (Element child : (Iterable<Element>)element.getChildren(CONFIGURATION_ELEMENT_NAME)) {
CONFIGURATIONS.add(createConfigurationOn(child));
}
@@ -89,13 +87,11 @@ public class CvsApplicationLevelConfiguration implements NamedComponent, JDOMExt
}
updateConfigurations();
}
private boolean encodingExists(String encoding) {
private static boolean encodingExists(String encoding) {
final Charset[] availableCharsets = CharsetToolkit.getAvailableCharsets();
for (int i = 0; i < availableCharsets.length; i++) {
Charset availableCharset = availableCharsets[i];
for (Charset availableCharset : availableCharsets) {
if (availableCharset.name().equals(encoding)) {
return true;
}
@@ -105,12 +101,12 @@ public class CvsApplicationLevelConfiguration implements NamedComponent, JDOMExt
public void writeExternal(Element element) throws WriteExternalException {
DefaultJDOMExternalizer.writeExternal(this, element);
for (Iterator each = CONFIGURATIONS.iterator(); each.hasNext();) {
createConfigurationElement((CvsRootConfiguration)each.next(), element);
for (CvsRootConfiguration configuration : CONFIGURATIONS) {
createConfigurationElement(configuration, element);
}
}
private void createConfigurationElement(CvsRootConfiguration configuration, Element element)
private static void createConfigurationElement(CvsRootConfiguration configuration, Element element)
throws WriteExternalException {
Element child = new Element(CONFIGURATION_ELEMENT_NAME);
configuration.writeExternal(child);
@@ -123,18 +119,13 @@ public class CvsApplicationLevelConfiguration implements NamedComponent, JDOMExt
return config;
}
@NonNls private String defaultPathToPassFile() {
return "$userdir" + "/.cvspass";
}
private boolean passFileExists() {
if (PATH_TO_PASSWORD_FILE == null) return false;
return new File(convertToIOFilePath(PATH_TO_PASSWORD_FILE)).isFile();
}
public static String convertToIOFilePath(String presentation) {
String userHome = System.getProperty(USER_HOME_PROPERTY);
userHome = userHome.replace(File.separatorChar, '/');
String userHome = SystemProperties.getUserHome().replace(File.separatorChar, '/');
presentation = presentation.replace(File.separatorChar, '/');
try {
String result = StringUtil.replace(presentation, "$userdir", userHome);
@@ -142,25 +133,24 @@ public class CvsApplicationLevelConfiguration implements NamedComponent, JDOMExt
return result;
}
catch (Exception ex) {
LOG.error("userHome = " + userHome + ", presenation = " + presentation);
LOG.error("userHome = " + userHome + ", presentation = " + presentation);
return "";
}
}
public String getPathToPassFile() {
return convertToIOFilePath(getPathToPassFilePresentation());
public File getPassFile() {
return new File(convertToIOFilePath(getPathToPassFilePresentation()));
}
public String getPathToPassFilePresentation() {
if (!passFileExists()) {
PATH_TO_PASSWORD_FILE = defaultPathToPassFile();
PATH_TO_PASSWORD_FILE = "$userdir" + "/.cvspass";
}
return PATH_TO_PASSWORD_FILE;
}
public CvsRootConfiguration getConfigurationForCvsRoot(String root) {
for (Iterator each = CONFIGURATIONS.iterator(); each.hasNext();) {
CvsRootConfiguration cvsRootConfiguration = (CvsRootConfiguration)each.next();
for (CvsRootConfiguration cvsRootConfiguration : CONFIGURATIONS) {
if (cvsRootConfiguration.getCvsRootAsString().equals(root)) {
return cvsRootConfiguration;
}
@@ -189,30 +179,29 @@ public class CvsApplicationLevelConfiguration implements NamedComponent, JDOMExt
}
private void updateConfigurations() {
final File passFile = new File(getPathToPassFile());
if (passFile.isFile()) {
final File passFile = getPassFile();
if (!passFile.isFile()) {
return;
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(passFile)));
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(passFile)));
try {
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("/1 ")) {
line = line.substring(3);
}
final int sepPosition = line.indexOf(' ');
if (sepPosition > 0) {
final String cvsRoot = line.substring(0, sepPosition);
tryToAddNewRoot(cvsRoot);
}
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("/1 ")) {
line = line.substring(3);
}
final int sepPosition = line.indexOf(' ');
if (sepPosition > 0) {
final String cvsRoot = line.substring(0, sepPosition);
tryToAddNewRoot(cvsRoot);
}
} finally {
reader.close();
}
} catch (IOException e) {
//ignore
} finally {
reader.close();
}
} catch (IOException e) {
//ignore
}
}
@@ -35,7 +35,6 @@ import org.netbeans.lib.cvsclient.connection.PServerPasswordScrambler;
import org.netbeans.lib.cvsclient.connection.UnknownUserException;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
@@ -83,13 +82,11 @@ public class PServerLoginProviderImpl extends PServerLoginProvider {
try {
connection.open(new StreamLogger());
mySettings.setOffline(false);
} catch (AuthenticationException e) {
if (e instanceof UnknownUserException) {
throw new SolveableAuthenticationException(e.getMessage(), e);
} else {
throw e;
}
} finally {
}
catch (UnknownUserException e) {
throw new SolveableAuthenticationException(e.getMessage(), e);
}
finally {
try {
connection.close();
}
@@ -104,10 +101,11 @@ public class PServerLoginProviderImpl extends PServerLoginProvider {
final String cvsRoot = mySettings.getCvsRootAsString();
final String password = requestForPassword(cvsRoot);
if (password == null) return false;
removeAllPasswordsForThisCvsRootFromPasswordFile(cvsRoot);
try {
removeAllPasswordsForThisCvsRootFromPasswordFile(cvsRoot);
storePassword(cvsRoot, password);
} catch (IOException e) {
}
catch (IOException e) {
showConnectionErrorMessage(myProject, CvsBundle.message("error.message.cannot.store.password", e.getLocalizedMessage()));
return false;
}
@@ -123,43 +121,15 @@ public class PServerLoginProviderImpl extends PServerLoginProvider {
// TODO do release password ! when opening a connection and there's a problem with authorization
private static ArrayList<String> readConfigurationNotMatchedWith(String cvsRoot, File passFile) {
FileInputStream input;
try {
input = new FileInputStream(passFile);
} catch (FileNotFoundException e) {
return new ArrayList<String>();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
ArrayList<String> result = new ArrayList<String>();
try {
String line;
while ((line = reader.readLine()) != null) {
if (line.indexOf(cvsRoot) == -1) result.add(line);
}
} catch (IOException ex) {
// ignore
} finally {
try {
reader.close();
} catch (IOException e) {
// ignore
}
}
return result;
}
private static void removeAllPasswordsForThisCvsRootFromPasswordFile(String cvsRoot) {
private static void removeAllPasswordsForThisCvsRootFromPasswordFile(String cvsRoot) throws IOException {
File passFile = getPassFile();
if (passFile == null) return;
if (!passFile.isFile()) return;
ArrayList<String> lines = readConfigurationNotMatchedWith(cvsRoot, passFile);
List<String> lines = CvsFileUtil.readLinesFrom(passFile, cvsRoot);
try {
CvsFileUtil.storeLines(lines, passFile);
} catch (IOException e) {
}
catch (IOException e) {
LOG.error(e);
}
}
@@ -176,19 +146,22 @@ public class PServerLoginProviderImpl extends PServerLoginProvider {
private static String getPassword(String config) {
File passFile = getPassFile();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(passFile)));
BufferedReader reader =
new BufferedReader(new InputStreamReader(new FileInputStream(passFile), CvsApplicationLevelConfiguration.getCharset()));
try {
return findPasswordIn(reader, config);
} finally {
}
finally {
reader.close();
}
} catch (IOException e) {
}
catch (IOException e) {
return null;
}
}
private static File getPassFile() {
return new File(CvsApplicationLevelConfiguration.getInstance().getPathToPassFile());
return CvsApplicationLevelConfiguration.getInstance().getPassFile();
}
@Nullable
@@ -48,6 +48,22 @@ public class CvsFileUtil {
}
}
public static List<String> readLinesFrom(File file, String cvsRootToSkip) throws IOException {
FileUtil.createIfDoesntExist(file);
ArrayList<String> result = new ArrayList<String>();
BufferedReader reader =
new BufferedReader(new InputStreamReader(new FileInputStream(file), CvsApplicationLevelConfiguration.getCharset()));
try {
String line;
while ((line = reader.readLine()) != null) {
if (!line.contains(cvsRootToSkip)) result.add(line);
}
return result;
} finally {
reader.close();
}
}
private static String getLineSeparatorFor(File file) {
VirtualFile virtualFile = CvsVfsUtil.findFileByIoFile(file);
if (virtualFile != null) {
@@ -59,18 +75,14 @@ public class CvsFileUtil {
}
public static void storeLines(List<String> lines, File file) throws IOException {
String separator = getLineSeparatorFor(file);
FileUtil.createIfDoesntExist(file);
if (!file.canWrite()) {
new FileReadOnlyHandler().setFileReadOnly(file, false);
}
Writer writer =
new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(file)), CvsApplicationLevelConfiguration.getCharset());
try {
for (final String line : lines) {
writer.write(line);