Initial work on new Status handler - SIDP-324
[java-idp.git] / src / main / java / edu / internet2 / middleware / shibboleth / idp / StatusServlet.java
1 /*
2  * Copyright 2009 University Corporation for Advanced Internet Development, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package edu.internet2.middleware.shibboleth.idp;
18
19 import java.io.IOException;
20 import java.io.PrintWriter;
21
22 import javax.servlet.ServletConfig;
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServlet;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.joda.time.DateTime;
29 import org.joda.time.chrono.ISOChronology;
30 import org.joda.time.format.DateTimeFormatter;
31 import org.joda.time.format.ISODateTimeFormat;
32 import org.opensaml.xml.util.DatatypeHelper;
33
34 import edu.internet2.middleware.shibboleth.common.attribute.resolver.AttributeResolutionException;
35 import edu.internet2.middleware.shibboleth.common.attribute.resolver.AttributeResolver;
36 import edu.internet2.middleware.shibboleth.common.relyingparty.RelyingPartyConfiguration;
37 import edu.internet2.middleware.shibboleth.common.relyingparty.RelyingPartyConfigurationManager;
38
39 /** A servlet for displaying the status of the IdP. */
40 public class StatusServlet extends HttpServlet {
41
42     /** Formatter used when print date/times. */
43     private DateTimeFormatter dateFormat;
44
45     /** Time the IdP started up. */
46     private DateTime startTime;
47
48     /** Attribute resolver used by the IdP. */
49     private AttributeResolver<?> attributeResolver;
50
51     /** Relying party configuration manager used by the IdP. */
52     private RelyingPartyConfigurationManager rpConfigManager;
53
54     /** {@inheritDoc} */
55     public void init(ServletConfig config) throws ServletException {
56         super.init(config);
57
58         dateFormat = ISODateTimeFormat.dateTimeNoMillis();
59
60         startTime = new DateTime(ISOChronology.getInstanceUTC());
61
62         String attributeResolverId = config.getInitParameter("attributeResolverId");
63         if (DatatypeHelper.isEmpty(attributeResolverId)) {
64             attributeResolverId = "shibboleth.AttributeResolver";
65         }
66         attributeResolver = (AttributeResolver<?>) getServletContext().getAttribute(attributeResolverId);
67
68         String rpConfigManagerId = config.getInitParameter("rpConfigManagerId");
69         if (DatatypeHelper.isEmpty(rpConfigManagerId)) {
70             rpConfigManagerId = "shibboleth.RelyingPartyConfigurationManager";
71         }
72         rpConfigManager = (RelyingPartyConfigurationManager) getServletContext().getAttribute(rpConfigManagerId);
73     }
74
75     /** {@inheritDoc} */
76     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
77         PrintWriter output = resp.getWriter();
78
79         printOperatingEnvironmentInformation(output);
80         output.println();
81         printIdPInformation(output);
82         output.println();
83         printRelyingPartyConfigurationsInformation(output, req.getParameter("relyingParty"));
84
85         output.flush();
86     }
87
88     /**
89      * Prints out information about the operating environment. This includes the operating system name, version and
90      * architecture, the JDK version, available CPU cores, memory currently used by the JVM process, the maximum amount
91      * of memory that may be used by the JVM, and the current time in UTC.
92      * 
93      * @param out output writer to which information will be written
94      */
95     protected void printOperatingEnvironmentInformation(PrintWriter out) {
96         Runtime runtime = Runtime.getRuntime();
97
98         out.println("Operating Environment Information");
99         out.println("operating_system: " + System.getProperty("os.name"));
100         out.println("operating_system_version: " + System.getProperty("os.version"));
101         out.println("operating_system_architecture: " + System.getProperty("os.arch"));
102         out.println("jdk_version: " + System.getProperty("java.version"));
103         out.println("available_cores: " + runtime.availableProcessors());
104         out.println("used_memory: " + runtime.totalMemory() / 1048576 + "MB");
105         out.println("maximum_memory: " + runtime.maxMemory() / 1048576 + "MB");
106         out.println("current_time: " + new DateTime(ISOChronology.getInstanceUTC()).toString(dateFormat));
107     }
108
109     /**
110      * Prints out general IdP information. This includes IdP version, start up time, and whether the attribute resolver
111      * is currently operational.
112      * 
113      * @param out output writer to which information will be written
114      */
115     protected void printIdPInformation(PrintWriter out) {
116         Package pkg = Version.class.getPackage();
117
118         out.println("Identity Provider Information");
119         out.println("idp_version: " + pkg.getImplementationVersion());
120         out.println("idp_start_time: " + startTime.toString(dateFormat));
121         try {
122             attributeResolver.validate();
123             out.println("attribute_resolver_valid: " + Boolean.TRUE);
124         } catch (AttributeResolutionException e) {
125             out.println("attribute_resolver_valid: " + Boolean.FALSE);
126         }
127     }
128
129     /**
130      * Prints information about relying party configurations. If the given relying party is null then the configuration
131      * for all relying parties is printed. If the relying party ID is not null then the relying party configurations for
132      * that entity is printed.
133      * 
134      * @param out output writer to which information will be written
135      * @param relyingPartyId entity ID of the relying party whose configuration should be printed
136      */
137     protected void printRelyingPartyConfigurationsInformation(PrintWriter out, String relyingPartyId) {
138         out.println("Relying Party Configurations");
139
140         if (relyingPartyId == null) {
141             for (RelyingPartyConfiguration config : rpConfigManager.getRelyingPartyConfigurations().values()) {
142                 printRelyingPartyConfigurationInformation(out, config);
143                 out.println();
144             }
145         } else {
146             RelyingPartyConfiguration config = rpConfigManager.getRelyingPartyConfiguration(relyingPartyId);
147             printRelyingPartyConfigurationInformation(out, config);
148             out.println();
149         }
150     }
151
152     /**
153      * Prints out the information for a specific relying party configuration. This information includes the relying
154      * party or relying party group ID, the entity ID of the IdP when it responds when using this configuration, the
155      * default authentication method used for this config, and configured communication profiles.
156      * 
157      * @param out output writer to which information will be written
158      * @param config the relying party configuration
159      */
160     protected void printRelyingPartyConfigurationInformation(PrintWriter out, RelyingPartyConfiguration config) {
161         out.println("relying_party_id: " + config.getRelyingPartyId());
162         out.println("idp_entity_id: " + config.getProviderId());
163
164         if (config.getDefaultAuthenticationMethod() != null) {
165             out.println("default_authentication_method: " + config.getDefaultAuthenticationMethod());
166         } else {
167             out.println("default_authentication_method: none");
168         }
169
170         for (String profileId : config.getProfileConfigurations().keySet()) {
171             out.println("configured_communication_profile: " + profileId);
172         }
173     }
174 }