mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2026-04-05 00:00:01 -04:00
moving files around
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (c) 2008, Harald Kuhr
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name "TwelveMonkeys" nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.twelvemonkeys.net;
|
||||
|
||||
import java.net.*;
|
||||
|
||||
/**
|
||||
* Interface for filtering Authenticator requests, used by the
|
||||
* SimpleAuthenticator.
|
||||
*
|
||||
* @see SimpleAuthenticator
|
||||
* @see java.net.Authenticator
|
||||
*
|
||||
* @author Harald Kuhr (haraldk@iconmedialab.no),
|
||||
* @version 1.0
|
||||
*/
|
||||
public interface AuthenticatorFilter {
|
||||
public boolean accept(InetAddress pAddress, int pPort, String pProtocol,
|
||||
String pPrompt, String pScheme);
|
||||
|
||||
}
|
||||
1103
common/common-io/src/main/java/com/twelvemonkeys/net/HttpURLConnection.java
Executable file
1103
common/common-io/src/main/java/com/twelvemonkeys/net/HttpURLConnection.java
Executable file
File diff suppressed because it is too large
Load Diff
312
common/common-io/src/main/java/com/twelvemonkeys/net/MIMEUtil.java
Executable file
312
common/common-io/src/main/java/com/twelvemonkeys/net/MIMEUtil.java
Executable file
@@ -0,0 +1,312 @@
|
||||
/*
|
||||
* Copyright (c) 2008, Harald Kuhr
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name "TwelveMonkeys" nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.twelvemonkeys.net;
|
||||
|
||||
import com.twelvemonkeys.lang.StringUtil;
|
||||
import com.twelvemonkeys.lang.SystemUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Contains mappings from file extension to mime-types and from mime-type to file-types.
|
||||
* <p/>
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/net/MIMEUtil.java#5 $
|
||||
*
|
||||
* @see <A href="http://www.iana.org/assignments/media-types/">MIME Media Types</A>
|
||||
*/
|
||||
public final class MIMEUtil {
|
||||
// TODO: Piggy-back on the mappings form the JRE? (1.6 comes with javax.activation)
|
||||
// TODO: Piggy-back on mappings from javax.activation?
|
||||
// See: http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/activation/MimetypesFileTypeMap.html
|
||||
// See: http://java.sun.com/javase/6/docs/api/javax/activation/MimetypesFileTypeMap.html
|
||||
// TODO: Use the format (and lookup) specified by the above URLs
|
||||
// TODO: Allow 3rd party to add mappings? Will need application context support to do it safe.. :-P
|
||||
|
||||
private static Map<String, List<String>> sExtToMIME = new HashMap<String, List<String>>();
|
||||
private static Map<String, List<String>> sUnmodifiableExtToMIME = Collections.unmodifiableMap(sExtToMIME);
|
||||
|
||||
private static Map<String, List<String>> sMIMEToExt = new HashMap<String, List<String>>();
|
||||
private static Map<String, List<String>> sUnmodifiableMIMEToExt = Collections.unmodifiableMap(sMIMEToExt);
|
||||
|
||||
static {
|
||||
// Load mapping for MIMEUtil
|
||||
try {
|
||||
Properties mappings = SystemUtil.loadProperties(MIMEUtil.class);
|
||||
|
||||
for (Map.Entry entry : mappings.entrySet()) {
|
||||
// Convert and break up extensions and mimeTypes
|
||||
String extStr = StringUtil.toLowerCase((String) entry.getKey());
|
||||
List<String> extensions =
|
||||
Collections.unmodifiableList(Arrays.asList(StringUtil.toStringArray(extStr, ";, ")));
|
||||
|
||||
String typeStr = StringUtil.toLowerCase((String) entry.getValue());
|
||||
List<String> mimeTypes =
|
||||
Collections.unmodifiableList(Arrays.asList(StringUtil.toStringArray(typeStr, ";, ")));
|
||||
|
||||
// TODO: Handle duplicates in MIME to extension mapping, like
|
||||
// xhtml=application/xhtml+xml;application/xml
|
||||
// xml=text/xml;application/xml
|
||||
|
||||
// Populate normal and reverse MIME-mappings
|
||||
for (String extension : extensions) {
|
||||
sExtToMIME.put(extension, mimeTypes);
|
||||
}
|
||||
|
||||
for (String mimeType : mimeTypes) {
|
||||
sMIMEToExt.put(mimeType, extensions);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
System.err.println("Could not read properties for MIMEUtil: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// Disallow construction
|
||||
private MIMEUtil() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default MIME type for the given file extension.
|
||||
*
|
||||
* @param pFileExt the file extension
|
||||
*
|
||||
* @return a {@code String} containing the MIME type, or {@code null} if
|
||||
* there are no known MIME types for the given file extension.
|
||||
*/
|
||||
public static String getMIMEType(final String pFileExt) {
|
||||
List<String> types = sExtToMIME.get(StringUtil.toLowerCase(pFileExt));
|
||||
return (types == null || types.isEmpty()) ? null : types.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all MIME types for the given file extension.
|
||||
*
|
||||
* @param pFileExt the file extension
|
||||
*
|
||||
* @return a {@link List} of {@code String}s containing the MIME types, or an empty
|
||||
* list, if there are no known MIME types for the given file extension.
|
||||
*/
|
||||
public static List<String> getMIMETypes(final String pFileExt) {
|
||||
List<String> types = sExtToMIME.get(StringUtil.toLowerCase(pFileExt));
|
||||
return maskNull(types);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an unmodifiabale {@link Map} view of the extension to
|
||||
* MIME mapping, to use as the default mapping in client applications.
|
||||
*
|
||||
* @return an unmodifiabale {@code Map} view of the extension to
|
||||
* MIME mapping.
|
||||
*/
|
||||
public static Map<String, List<String>> getMIMETypeMappings() {
|
||||
return sUnmodifiableExtToMIME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default file extension for the given MIME type.
|
||||
* Specifying a wildcard type will return {@code null}.
|
||||
*
|
||||
* @param pMIME the MIME type
|
||||
*
|
||||
* @return a {@code String} containing the file extension, or {@code null}
|
||||
* if there are no known file extensions for the given MIME type.
|
||||
*/
|
||||
public static String getExtension(final String pMIME) {
|
||||
String mime = bareMIME(StringUtil.toLowerCase(pMIME));
|
||||
List<String> extensions = sMIMEToExt.get(mime);
|
||||
return (extensions == null || extensions.isEmpty()) ? null : extensions.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all file extension for the given MIME type.
|
||||
* The default extension will be the first in the list.
|
||||
* Note that no specific order is given for wildcard types (image/*, */* etc).
|
||||
*
|
||||
* @param pMIME the MIME type
|
||||
*
|
||||
* @return a {@link List} of {@code String}s containing the MIME types, or an empty
|
||||
* list, if there are no known file extensions for the given MIME type.
|
||||
*/
|
||||
public static List<String> getExtensions(final String pMIME) {
|
||||
String mime = bareMIME(StringUtil.toLowerCase(pMIME));
|
||||
if (mime.endsWith("/*")) {
|
||||
return getExtensionForWildcard(mime);
|
||||
}
|
||||
List<String> extensions = sMIMEToExt.get(mime);
|
||||
return maskNull(extensions);
|
||||
}
|
||||
|
||||
// Gets all extensions for a wildcard MIME type
|
||||
private static List<String> getExtensionForWildcard(final String pMIME) {
|
||||
final String family = pMIME.substring(0, pMIME.length() - 1);
|
||||
Set<String> extensions = new LinkedHashSet<String>();
|
||||
for (Map.Entry<String, List<String>> mimeToExt : sMIMEToExt.entrySet()) {
|
||||
if ("*/".equals(family) || mimeToExt.getKey().startsWith(family)) {
|
||||
extensions.addAll(mimeToExt.getValue());
|
||||
}
|
||||
}
|
||||
return Collections.unmodifiableList(new ArrayList<String>(extensions));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an unmodifiabale {@link Map} view of the MIME to
|
||||
* extension mapping, to use as the default mapping in client applications.
|
||||
*
|
||||
* @return an unmodifiabale {@code Map} view of the MIME to
|
||||
* extension mapping.
|
||||
*/
|
||||
public static Map<String, List<String>> getExtensionMappings() {
|
||||
return sUnmodifiableMIMEToExt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests wehter the type is a subtype of the type family.
|
||||
*
|
||||
* @param pTypeFamily the MIME type family ({@code image/*, */*}, etc)
|
||||
* @param pType the MIME type
|
||||
* @return {@code true} if {@code pType} is a subtype of {@code pTypeFamily}, otherwise {@code false}
|
||||
*/
|
||||
// TODO: Rename? isSubtype?
|
||||
// TODO: Make public
|
||||
static boolean includes(final String pTypeFamily, final String pType) {
|
||||
// TODO: Handle null in a well-defined way
|
||||
// - Is null family same as */*?
|
||||
// - Is null subtype of any family? Subtype of no family?
|
||||
|
||||
String type = bareMIME(pType);
|
||||
return type.equals(pTypeFamily)
|
||||
|| "*/*".equals(pTypeFamily)
|
||||
|| pTypeFamily.endsWith("/*") && pTypeFamily.startsWith(type.substring(0, type.indexOf('/')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes any charset or extra info from the mime-type string (anything after a semicolon, {@code ;}, inclusive).
|
||||
*
|
||||
* @param pMIME the mime-type string
|
||||
* @return the bare mime-type
|
||||
*/
|
||||
public static String bareMIME(final String pMIME) {
|
||||
int idx;
|
||||
if (pMIME != null && (idx = pMIME.indexOf(';')) >= 0) {
|
||||
return pMIME.substring(0, idx);
|
||||
}
|
||||
return pMIME;
|
||||
}
|
||||
|
||||
// Returns the list or empty list if list is null
|
||||
private static List<String> maskNull(List<String> pTypes) {
|
||||
return (pTypes == null) ? Collections.<String>emptyList() : pTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* For debugging. Prints all known MIME types and file extensions.
|
||||
*
|
||||
* @param pArgs command line arguments
|
||||
*/
|
||||
public static void main(String[] pArgs) {
|
||||
if (pArgs.length > 1) {
|
||||
String type = pArgs[0];
|
||||
String family = pArgs[1];
|
||||
boolean incuded = includes(family, type);
|
||||
System.out.println(
|
||||
"Mime type family " + family
|
||||
+ (incuded ? " includes " : " does not include ")
|
||||
+ "type " + type
|
||||
);
|
||||
}
|
||||
if (pArgs.length > 0) {
|
||||
String str = pArgs[0];
|
||||
|
||||
if (str.indexOf('/') >= 0) {
|
||||
// MIME
|
||||
String extension = getExtension(str);
|
||||
System.out.println("Default extension for MIME type '" + str + "' is "
|
||||
+ (extension != null ? ": '" + extension + "'" : "unknown") + ".");
|
||||
System.out.println("All possible: " + getExtensions(str));
|
||||
}
|
||||
else {
|
||||
// EXT
|
||||
String mimeType = getMIMEType(str);
|
||||
System.out.println("Default MIME type for extension '" + str + "' is "
|
||||
+ (mimeType != null ? ": '" + mimeType + "'" : "unknown") + ".");
|
||||
System.out.println("All possible: " + getMIMETypes(str));
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Set set = sMIMEToExt.keySet();
|
||||
String[] mimeTypes = new String[set.size()];
|
||||
int i = 0;
|
||||
for (Iterator iterator = set.iterator(); iterator.hasNext(); i++) {
|
||||
String mime = (String) iterator.next();
|
||||
mimeTypes[i] = mime;
|
||||
}
|
||||
Arrays.sort(mimeTypes);
|
||||
|
||||
System.out.println("Known MIME types (" + mimeTypes.length + "):");
|
||||
for (int j = 0; j < mimeTypes.length; j++) {
|
||||
String mimeType = mimeTypes[j];
|
||||
|
||||
if (j != 0) {
|
||||
System.out.print(", ");
|
||||
}
|
||||
|
||||
System.out.print(mimeType);
|
||||
}
|
||||
|
||||
System.out.println("\n");
|
||||
|
||||
set = sExtToMIME.keySet();
|
||||
String[] extensions = new String[set.size()];
|
||||
i = 0;
|
||||
for (Iterator iterator = set.iterator(); iterator.hasNext(); i++) {
|
||||
String ext = (String) iterator.next();
|
||||
extensions[i] = ext;
|
||||
}
|
||||
Arrays.sort(extensions);
|
||||
|
||||
System.out.println("Known file types (" + extensions.length + "):");
|
||||
for (int j = 0; j < extensions.length; j++) {
|
||||
String extension = extensions[j];
|
||||
|
||||
if (j != 0) {
|
||||
System.out.print(", ");
|
||||
}
|
||||
|
||||
System.out.print(extension);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
1423
common/common-io/src/main/java/com/twelvemonkeys/net/NetUtil.java
Executable file
1423
common/common-io/src/main/java/com/twelvemonkeys/net/NetUtil.java
Executable file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2008, Harald Kuhr
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name "TwelveMonkeys" nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.twelvemonkeys.net;
|
||||
|
||||
import java.net.*;
|
||||
|
||||
/**
|
||||
* Interface fro PasswordAuthenticators used by SimpleAuthenticator.
|
||||
*
|
||||
* @see SimpleAuthenticator
|
||||
* @see java.net.Authenticator
|
||||
*
|
||||
* @author Harald Kuhr (haraldk@iconmedialab.no)
|
||||
*
|
||||
* @version 1.0
|
||||
*/
|
||||
public interface PasswordAuthenticator {
|
||||
public PasswordAuthentication requestPasswordAuthentication(InetAddress addr, int port, String protocol, String prompt, String scheme);
|
||||
}
|
||||
326
common/common-io/src/main/java/com/twelvemonkeys/net/SimpleAuthenticator.java
Executable file
326
common/common-io/src/main/java/com/twelvemonkeys/net/SimpleAuthenticator.java
Executable file
@@ -0,0 +1,326 @@
|
||||
/*
|
||||
* Copyright (c) 2008, Harald Kuhr
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name "TwelveMonkeys" nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.twelvemonkeys.net;
|
||||
|
||||
import java.net.Authenticator;
|
||||
import java.net.InetAddress;
|
||||
import java.net.PasswordAuthentication;
|
||||
import java.net.URL;
|
||||
import java.util.Hashtable;
|
||||
|
||||
/**
|
||||
* A simple Authenticator implementation.
|
||||
* Singleton class, obtain reference through the static
|
||||
* {@code getInstance} method.
|
||||
* <P>
|
||||
* <EM>After swearing, sweating, pulling my hair, banging my head repeatedly
|
||||
* into the walls and reading the java.net.Authenticator API documentation
|
||||
* once more, an idea came to my mind. This is the result. I hope you find it
|
||||
* useful. -- Harald K.</EM>
|
||||
*
|
||||
* @see java.net.Authenticator
|
||||
*
|
||||
* @author Harald Kuhr (haraldk@iconmedialab.no)
|
||||
* @version 1.0
|
||||
*/
|
||||
public class SimpleAuthenticator extends Authenticator {
|
||||
|
||||
/** The reference to the single instance of this class. */
|
||||
private static SimpleAuthenticator sInstance = null;
|
||||
/** Keeps track of the state of this class. */
|
||||
private static boolean sInitialized = false;
|
||||
|
||||
// These are used for the identification hack.
|
||||
private final static String MAGIC = "magic";
|
||||
private final static int FOURTYTWO = 42;
|
||||
|
||||
/** Basic authentication scheme. */
|
||||
public final static String BASIC = "Basic";
|
||||
|
||||
/**
|
||||
* The hastable that keeps track of the PasswordAuthentications.
|
||||
*/
|
||||
|
||||
protected Hashtable mPasswordAuthentications = null;
|
||||
|
||||
/**
|
||||
* The hastable that keeps track of the Authenticators.
|
||||
*/
|
||||
|
||||
protected Hashtable mAuthenticators = null;
|
||||
|
||||
/**
|
||||
* Creates a SimpleAuthenticator.
|
||||
*/
|
||||
|
||||
private SimpleAuthenticator() {
|
||||
mPasswordAuthentications = new Hashtable();
|
||||
mAuthenticators = new Hashtable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the SimpleAuthenticator instance and registers it through the
|
||||
* Authenticator.setDefault(). If there is no current instance
|
||||
* of the SimpleAuthenticator in the VM, one is created. This method will
|
||||
* try to figure out if the setDefault() succeeded (a hack), and will
|
||||
* return null if it was not able to register the instance as default.
|
||||
*
|
||||
* @return The single instance of this class, or null, if another
|
||||
* Authenticator is allready registered as default.
|
||||
*/
|
||||
|
||||
public static synchronized SimpleAuthenticator getInstance() {
|
||||
if (!sInitialized) {
|
||||
// Create an instance
|
||||
sInstance = new SimpleAuthenticator();
|
||||
|
||||
// Try to set default (this may quietly fail...)
|
||||
Authenticator.setDefault(sInstance);
|
||||
|
||||
// A hack to figure out if we really did set the authenticator
|
||||
PasswordAuthentication pa =
|
||||
Authenticator.requestPasswordAuthentication(null, FOURTYTWO,
|
||||
null, null, MAGIC);
|
||||
|
||||
// If this test returns false, we didn't succeed, so we set the
|
||||
// instance back to null.
|
||||
if (pa == null || !MAGIC.equals(pa.getUserName()) ||
|
||||
!("" + FOURTYTWO).equals(new String(pa.getPassword())))
|
||||
sInstance = null;
|
||||
|
||||
// Done
|
||||
sInitialized = true;
|
||||
}
|
||||
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the PasswordAuthentication for the request. Called when password
|
||||
* authorization is needed.
|
||||
*
|
||||
* @return The PasswordAuthentication collected from the user, or null if
|
||||
* none is provided.
|
||||
*/
|
||||
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
// Don't worry, this is just a hack to figure out if we were able
|
||||
// to set this Authenticator through the setDefault method.
|
||||
if (!sInitialized && MAGIC.equals(getRequestingScheme())
|
||||
&& getRequestingPort() == FOURTYTWO)
|
||||
return new PasswordAuthentication(MAGIC, ("" + FOURTYTWO)
|
||||
.toCharArray());
|
||||
/*
|
||||
System.err.println("getPasswordAuthentication");
|
||||
System.err.println(getRequestingSite());
|
||||
System.err.println(getRequestingPort());
|
||||
System.err.println(getRequestingProtocol());
|
||||
System.err.println(getRequestingPrompt());
|
||||
System.err.println(getRequestingScheme());
|
||||
*/
|
||||
|
||||
// TODO:
|
||||
// Look for a more specific PasswordAuthenticatior before using
|
||||
// Default:
|
||||
//
|
||||
// if (...)
|
||||
// return pa.requestPasswordAuthentication(getRequestingSite(),
|
||||
// getRequestingPort(),
|
||||
// getRequestingProtocol(),
|
||||
// getRequestingPrompt(),
|
||||
// getRequestingScheme());
|
||||
|
||||
return (PasswordAuthentication)
|
||||
mPasswordAuthentications.get(new AuthKey(getRequestingSite(),
|
||||
getRequestingPort(),
|
||||
getRequestingProtocol(),
|
||||
getRequestingPrompt(),
|
||||
getRequestingScheme()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a PasswordAuthentication with a given URL address.
|
||||
*
|
||||
*/
|
||||
|
||||
public PasswordAuthentication registerPasswordAuthentication(URL pURL,
|
||||
PasswordAuthentication pPA) {
|
||||
return registerPasswordAuthentication(NetUtil.createInetAddressFromURL(pURL),
|
||||
pURL.getPort(),
|
||||
pURL.getProtocol(),
|
||||
null, // Prompt/Realm
|
||||
BASIC,
|
||||
pPA);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a PasswordAuthentication with a given net address.
|
||||
*
|
||||
*/
|
||||
|
||||
public PasswordAuthentication registerPasswordAuthentication(
|
||||
InetAddress pAddress, int pPort, String pProtocol,
|
||||
String pPrompt, String pScheme, PasswordAuthentication pPA)
|
||||
{
|
||||
/*
|
||||
System.err.println("registerPasswordAuthentication");
|
||||
System.err.println(pAddress);
|
||||
System.err.println(pPort);
|
||||
System.err.println(pProtocol);
|
||||
System.err.println(pPrompt);
|
||||
System.err.println(pScheme);
|
||||
*/
|
||||
|
||||
return (PasswordAuthentication)
|
||||
mPasswordAuthentications.put(new AuthKey(pAddress, pPort,
|
||||
pProtocol, pPrompt,
|
||||
pScheme),
|
||||
pPA);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters a PasswordAuthentication with a given URL address.
|
||||
*
|
||||
*/
|
||||
|
||||
public PasswordAuthentication unregisterPasswordAuthentication(URL pURL) {
|
||||
return unregisterPasswordAuthentication(NetUtil.createInetAddressFromURL(pURL),
|
||||
pURL.getPort(),
|
||||
pURL.getProtocol(),
|
||||
null,
|
||||
BASIC);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters a PasswordAuthentication with a given net address.
|
||||
*
|
||||
*/
|
||||
|
||||
public PasswordAuthentication unregisterPasswordAuthentication(
|
||||
InetAddress pAddress, int pPort, String pProtocol,
|
||||
String pPrompt, String pScheme)
|
||||
{
|
||||
return (PasswordAuthentication)
|
||||
mPasswordAuthentications.remove(new AuthKey(pAddress, pPort,
|
||||
pProtocol, pPrompt,
|
||||
pScheme));
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: Registers a PasswordAuthenticator that can answer authentication
|
||||
* requests.
|
||||
*
|
||||
* @see PasswordAuthenticator
|
||||
*/
|
||||
|
||||
public void registerPasswordAuthenticator(PasswordAuthenticator pPA,
|
||||
AuthenticatorFilter pFilter) {
|
||||
mAuthenticators.put(pPA, pFilter);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: Unregisters a PasswordAuthenticator that can answer authentication
|
||||
* requests.
|
||||
*
|
||||
* @see PasswordAuthenticator
|
||||
*/
|
||||
|
||||
public void unregisterPasswordAuthenticator(PasswordAuthenticator pPA) {
|
||||
mAuthenticators.remove(pPA);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility class, used for caching the PasswordAuthentication objects.
|
||||
* Everything but address may be null
|
||||
*/
|
||||
|
||||
class AuthKey {
|
||||
|
||||
InetAddress mAddress = null;
|
||||
int mPort = -1;
|
||||
String mProtocol = null;
|
||||
String mPrompt = null;
|
||||
String mScheme = null;
|
||||
|
||||
AuthKey(InetAddress pAddress, int pPort, String pProtocol,
|
||||
String pPrompt, String pScheme) {
|
||||
if (pAddress == null)
|
||||
throw new IllegalArgumentException("Address argument can't be null!");
|
||||
|
||||
mAddress = pAddress;
|
||||
mPort = pPort;
|
||||
mProtocol = pProtocol;
|
||||
mPrompt = pPrompt;
|
||||
mScheme = pScheme;
|
||||
|
||||
// System.out.println("Created: " + this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string representation of this object.
|
||||
*/
|
||||
|
||||
public String toString() {
|
||||
return "AuthKey[" + mAddress + ":" + mPort + "/" + mProtocol + " \"" + mPrompt + "\" (" + mScheme + ")]";
|
||||
}
|
||||
|
||||
public boolean equals(Object pObj) {
|
||||
return (pObj instanceof AuthKey ? equals((AuthKey) pObj) : false);
|
||||
}
|
||||
|
||||
// Ahem.. Breaks the rule from Object.equals(Object):
|
||||
// It is transitive: for any reference values x, y, and z, if x.equals(y)
|
||||
// returns true and y.equals(z) returns true, then x.equals(z)
|
||||
// should return true.
|
||||
|
||||
public boolean equals(AuthKey pKey) {
|
||||
// Maybe allow nulls, and still be equal?
|
||||
return (mAddress.equals(pKey.mAddress)
|
||||
&& (mPort == -1
|
||||
|| pKey.mPort == -1
|
||||
|| mPort == pKey.mPort)
|
||||
&& (mProtocol == null
|
||||
|| pKey.mProtocol == null
|
||||
|| mProtocol.equals(pKey.mProtocol))
|
||||
&& (mPrompt == null
|
||||
|| pKey.mPrompt == null
|
||||
|| mPrompt.equals(pKey.mPrompt))
|
||||
&& (mScheme == null
|
||||
|| pKey.mScheme == null
|
||||
|| mScheme.equalsIgnoreCase(pKey.mScheme)));
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
// There won't be too many pr address, will it? ;-)
|
||||
return mAddress.hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
4
common/common-io/src/main/java/com/twelvemonkeys/net/package_info.java
Executable file
4
common/common-io/src/main/java/com/twelvemonkeys/net/package_info.java
Executable file
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Provides classes for net access.
|
||||
*/
|
||||
package com.twelvemonkeys.net;
|
||||
Reference in New Issue
Block a user