Source Code Cross Referenced for CertReader.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » tools » keytool » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.tools.keytool 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         * http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013:         * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014:         * License for the specific language governing permissions and limitations under
015:         * the License.
016:         */
017:
018:        package org.apache.harmony.tools.keytool;
019:
020:        import java.io.FileInputStream;
021:        import java.io.FileNotFoundException;
022:        import java.io.IOException;
023:        import java.io.InputStream;
024:        import java.security.NoSuchProviderException;
025:        import java.security.cert.CRLException;
026:        import java.security.cert.CertificateException;
027:        import java.security.cert.CertificateFactory;
028:        import java.util.Collection;
029:        import java.util.Collections;
030:
031:        /**
032:         * Class for reading an X.509 certificate or an X.509 certificate chain from the
033:         * file or standard input.
034:         */
035:        public class CertReader {
036:            // certificate factory to read certificates and CRLs
037:            private static CertificateFactory certFactory;
038:            // time to wait for user to input the data.
039:            // need this for user to have time to paste the certificate to stdin.
040:            private static long sleepPeriod;
041:
042:            /**
043:             * Reads an X.509 certificate or a certificate chain from the file with the
044:             * given name or from stdin if the fileName is null and generates a
045:             * collection of Certificates.
046:             * 
047:             * @param fileName
048:             * @param readOnlyFirst
049:             * @param providerName
050:             * @return
051:             * @throws NoSuchProviderException 
052:             * @throws CertificateException 
053:             * @throws IOException 
054:             */
055:            static Collection readCerts(String fileName, boolean readOnlyFirst,
056:                    String providerName) throws CertificateException,
057:                    NoSuchProviderException, IOException {
058:
059:                InputStream input = getInputStream(fileName);
060:                CertificateFactory factory = getCertificateFactory(providerName);
061:                if (input == System.in) {
062:                    System.out.println("Please, input certificate(s)...");
063:                }
064:                try {
065:                    // let the user paste the certificates or CRLs, if read from stdin.
066:                    // If reading from file, don't sleep.
067:                    Thread.sleep(sleepPeriod);
068:                } catch (InterruptedException e) {
069:                    // do nothing
070:                }
071:
072:                // if the file is empty or nothing was entered
073:                // FIXME: remove available. Try to read and catch exception?
074:                if (input.available() <= 0) {
075:                    throw new IOException("Empty input");
076:                }
077:
078:                Collection certCollection;
079:                try {
080:                    // if only the first certificate is requested, return a
081:                    // single-element Collection
082:                    if (readOnlyFirst) {
083:                        certCollection = Collections.singleton(factory
084:                                .generateCertificate(input));
085:                    } else {
086:                        certCollection = factory.generateCertificates(input);
087:                    }
088:                    if (input != System.in) {
089:                        input.close();
090:                    }
091:                    return certCollection;
092:                } catch (CertificateException e) {
093:                    throw new CertificateException(
094:                            "Failed to generate a certificate from the input. ",
095:                            e);
096:                }
097:            }
098:
099:            /**
100:             * Reads CRLs from the file with given name and generates a collection of
101:             * CRLs.
102:             * 
103:             * @param fileName
104:             * @param providerName
105:             * @return
106:             * @throws NoSuchProviderException
107:             * @throws CertificateException
108:             * @throws IOException
109:             * @throws CRLException
110:             * 
111:             */
112:            static Collection readCRLs(String fileName, String providerName)
113:                    throws CertificateException, NoSuchProviderException,
114:                    IOException, CRLException {
115:
116:                InputStream input = getInputStream(fileName);
117:                CertificateFactory factory = getCertificateFactory(providerName);
118:                if (input == System.in) {
119:                    System.out.println("Please, input CRL(s)...");
120:                }
121:                try {
122:                    // let the user paste the certificates or CRLs, if read from stdin.
123:                    // If reading from file, don't sleep.
124:                    Thread.sleep(sleepPeriod);
125:                } catch (InterruptedException e) {
126:                    // do nothing
127:                }
128:
129:                // if the file is empty or nothing was entered
130:                // FIXME: remove available. Try to read and catch exception?
131:                if (input.available() <= 0) {
132:                    throw new IOException("Empty input");
133:                }
134:
135:                try {
136:                    Collection crlCollection = factory.generateCRLs(input);
137:                    if (input != System.in) {
138:                        input.close();
139:                    }
140:                    return crlCollection;
141:                } catch (CRLException e) {
142:                    throw new CRLException(
143:                            "Failed to generate a CRL from the input. ", e);
144:                }
145:            }
146:
147:            // Returns an input stream - FileInputStream or System.in.
148:            private static InputStream getInputStream(String fileName)
149:                    throws FileNotFoundException {
150:                if (fileName != null) {
151:                    sleepPeriod = 0;
152:                    // use the file if its name is specified
153:                    return new FileInputStream(fileName);
154:                } else {// if the file name is not given, use stdin
155:                    sleepPeriod = 3000;
156:                    return System.in;
157:                }
158:            }
159:
160:            // Sets certFactory if it is still not set and returns it
161:            private static CertificateFactory getCertificateFactory(
162:                    String providerName) throws CertificateException,
163:                    NoSuchProviderException {
164:                if (certFactory == null) {
165:                    try {
166:                        if (providerName == null) {
167:                            certFactory = CertificateFactory
168:                                    .getInstance("X.509");
169:                        } else {
170:                            certFactory = CertificateFactory.getInstance(
171:                                    "X.509", providerName);
172:                        }
173:                    } catch (CertificateException e) {
174:                        throw new CertificateException(
175:                                "This type of certificate is not "
176:                                        + "available from the provider. ", e);
177:                    } catch (NoSuchProviderException e) {
178:                        throw (NoSuchProviderException) new NoSuchProviderException(
179:                                "The provider " + providerName
180:                                        + " is not found in the environment.")
181:                                .initCause(e);
182:                    }
183:                }
184:                return certFactory;
185:            }
186:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.