use the new session manager interface
[java-idp.git] / tests / edu / internet2 / middleware / shibboleth / utils / MockObjectUtils.java
1 /*
2  * Copyright [2005] [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 package edu.internet2.middleware.shibboleth.utils;
17
18 import java.io.FileInputStream;
19 import java.security.cert.CertificateFactory;
20 import java.security.cert.X509Certificate;
21 import java.util.Collection;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 import com.mockrunner.mock.web.MockHttpServletRequest;
26 import com.mockrunner.mock.web.MockHttpServletResponse;
27
28 /**
29  * Assorted convenience methods for working with MockRunner
30  * 
31  * @author Will Norris (wnorris@memphis.edu)
32  */
33 public class MockObjectUtils {
34
35         /**
36          * Set the client SSL certificate for the given request object
37          * 
38          * @param request
39          * @param certFile
40          *            path to client SSL certificate
41          * @throws Exception
42          */
43         public static void setClientCert(MockHttpServletRequest request,
44                         String certFile) throws Exception {
45                 FileInputStream fis = new FileInputStream(certFile);
46                 CertificateFactory cf = CertificateFactory.getInstance("X.509");
47                 Collection c = cf.generateCertificates(fis);
48
49                 X509Certificate[] certs = new X509Certificate[c.size()];
50                 certs = (X509Certificate[]) c.toArray(certs);
51
52                 request.setAttribute("javax.servlet.request.X509Certificate", certs);
53         }
54
55         /**
56          * Get SAML status message from the given response object
57          * 
58          * @param response
59          * @return
60          */
61         public static String getSamlStatusMessage(MockHttpServletResponse response) {
62                 Pattern p = Pattern.compile("<StatusMessage>([^<]*)</StatusMessage>");
63                 Matcher m = p.matcher(response.getOutputStreamContent());
64                 if (m.find()) {
65                         return m.group(1);
66                 } else {
67                         return null;
68                 }
69         }
70 }