325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.tools.internal.ws.wscompile;
325N/A
325N/Aimport com.sun.istack.internal.NotNull;
325N/Aimport com.sun.tools.internal.ws.resources.WscompileMessages;
325N/Aimport org.xml.sax.SAXParseException;
325N/Aimport org.xml.sax.helpers.LocatorImpl;
325N/A
325N/Aimport java.io.*;
325N/Aimport java.net.Authenticator;
325N/Aimport java.net.PasswordAuthentication;
325N/Aimport java.net.URL;
325N/Aimport java.util.ArrayList;
325N/Aimport java.util.List;
325N/A
325N/A/**
325N/A * @author Vivek Pandey
325N/A */
325N/Apublic class DefaultAuthenticator extends Authenticator {
325N/A
325N/A private final List<AuthInfo> authInfo = new ArrayList<AuthInfo>();
325N/A private final ErrorReceiver errReceiver;
325N/A private final String proxyUser;
325N/A private final String proxyPasswd;
325N/A
325N/A //can user.home value be null?
325N/A public static final String defaultAuthfile = System.getProperty("user.home")+ System.getProperty("file.separator")+".metro"+System.getProperty("file.separator")+"auth";
325N/A private File authFile = new File(defaultAuthfile);
325N/A private boolean giveError;
325N/A
325N/A public DefaultAuthenticator(@NotNull ErrorReceiver receiver, @NotNull File authfile) throws BadCommandLineException {
325N/A this.errReceiver = receiver;
325N/A this.proxyUser = System.getProperty("http.proxyUser");
325N/A this.proxyPasswd = System.getProperty("http.proxyPassword");
325N/A
325N/A if(authfile != null){
325N/A this.authFile = authfile;
325N/A this.giveError = true;
325N/A }
325N/A
325N/A if(!authFile.exists()){
325N/A try {
325N/A error(new SAXParseException(WscompileMessages.WSIMPORT_AUTH_FILE_NOT_FOUND(authFile.getCanonicalPath(), defaultAuthfile), null));
325N/A } catch (IOException e) {
325N/A error(new SAXParseException(WscompileMessages.WSIMPORT_FAILED_TO_PARSE(authFile,e.getMessage()), null));
325N/A }
325N/A return;
325N/A }
325N/A
325N/A if(!authFile.canRead()){
325N/A error(new SAXParseException("Authorization file: "+authFile + " does not have read permission!", null));
325N/A return;
325N/A }
325N/A parseAuth();
325N/A }
325N/A
325N/A protected PasswordAuthentication getPasswordAuthentication() {
325N/A //If user sets proxy user and passwd and the RequestType is from proxy server then create
325N/A // PasswordAuthentication using proxyUser and proxyClass;
325N/A if((getRequestorType() == RequestorType.PROXY) && proxyUser != null && proxyPasswd != null){
325N/A return new PasswordAuthentication(proxyUser, proxyPasswd.toCharArray());
325N/A }
325N/A for(AuthInfo auth:authInfo){
325N/A if(auth.matchingHost(getRequestingURL())){
325N/A return new PasswordAuthentication(auth.getUser(), auth.getPassword().toCharArray());
325N/A }
325N/A }
325N/A return null;
325N/A }
325N/A
325N/A private void parseAuth() {
325N/A errReceiver.info(new SAXParseException(WscompileMessages.WSIMPORT_READING_AUTH_FILE(authFile), null));
325N/A
325N/A BufferedReader in;
325N/A try {
325N/A in = new BufferedReader(new InputStreamReader(new FileInputStream(authFile), "UTF-8"));
325N/A } catch (UnsupportedEncodingException e) {
325N/A error(new SAXParseException(e.getMessage(), null));
325N/A return;
325N/A } catch (FileNotFoundException e) {
325N/A error(new SAXParseException(WscompileMessages.WSIMPORT_AUTH_FILE_NOT_FOUND(authFile, defaultAuthfile), null, e));
325N/A return;
325N/A }
325N/A String text;
325N/A LocatorImpl locator = new LocatorImpl();
325N/A try {
325N/A int lineno = 1;
325N/A
325N/A locator.setSystemId(authFile.getCanonicalPath());
325N/A
325N/A while ((text = in.readLine()) != null) {
325N/A locator.setLineNumber(lineno++);
325N/A try {
325N/A URL url = new URL(text);
325N/A String authinfo = url.getUserInfo();
325N/A
325N/A if (authinfo != null) {
325N/A int i = authinfo.indexOf(':');
325N/A
325N/A if (i >= 0) {
325N/A String user = authinfo.substring(0, i);
325N/A String password = authinfo.substring(i + 1);
325N/A authInfo.add(new AuthInfo(new URL(text), user, password));
325N/A } else {
325N/A error(new SAXParseException(WscompileMessages.WSIMPORT_ILLEGAL_AUTH_INFO(url), locator));
325N/A }
325N/A } else {
325N/A error(new SAXParseException(WscompileMessages.WSIMPORT_ILLEGAL_AUTH_INFO(url), locator));
325N/A }
325N/A
325N/A } catch (NumberFormatException e) {
325N/A error(new SAXParseException(WscompileMessages.WSIMPORT_ILLEGAL_AUTH_INFO(text), locator));
325N/A }
325N/A }
325N/A in.close();
325N/A } catch (IOException e) {
325N/A error(new SAXParseException(WscompileMessages.WSIMPORT_FAILED_TO_PARSE(authFile,e.getMessage()), locator));
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * When user provides authfile explicitly using -Xauthfile we throw error otherwise show the mesage by default with -Xdebug flag
325N/A */
325N/A private void error(SAXParseException e){
325N/A if(giveError){
325N/A errReceiver.error(e);
325N/A } else{
325N/A errReceiver.debug(e);
325N/A }
325N/A }
325N/A}