View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
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 org.gwe.app.client.web.view;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.gwe.app.client.config.ClientConfig;
23  import org.gwe.utils.security.AccessControl;
24  import org.gwe.utils.security.KeyStore;
25  import org.gwe.utils.security.Realm;
26  import org.gwe.utils.security.RealmTestResult;
27  import org.gwe.utils.web.HtmlTable;
28  import org.gwe.utils.web.HtmlTableCell;
29  import org.gwe.utils.web.WebIcon;
30  
31  /**
32   * @author Marco Ruiz
33   * @since Dec 21, 2008
34   */
35  public class HtmlKeysTable extends HtmlTable {
36  
37  	private static Map<RealmTestResult, WebIcon> STATUS_IMAGES = new HashMap<RealmTestResult, WebIcon>();
38  	
39  	static {
40  		STATUS_IMAGES.put(RealmTestResult.OK, WebIcon.STATUS_OK);
41  		STATUS_IMAGES.put(RealmTestResult.ERROR, WebIcon.STATUS_ERROR);
42  		STATUS_IMAGES.put(RealmTestResult.NO_SSH_REALM, WebIcon.STATUS_HELP);
43  		STATUS_IMAGES.put(RealmTestResult.MISSING_TEST_HOST, WebIcon.STATUS_HELP);
44  		STATUS_IMAGES.put(RealmTestResult.MISSING_ACCOUNT, WebIcon.STATUS_WARN);
45  		STATUS_IMAGES.put(RealmTestResult.MISSING_PASSKEY, WebIcon.STATUS_WARN);
46  	}
47  	
48  	public HtmlKeysTable(ClientConfig config) {
49  	    super("User Name", "Authentication", "Domains", "Test Host", "");
50  	    
51  	    KeyStore keys = config.getKeys();
52  		for (AccessControl ac : keys.getAccessControls()) {
53  	        for (Realm realm : ac.getRealms()) {
54  	        	HtmlTableCell status = new HtmlTableCell("Tested!", "", null, STATUS_IMAGES.get(realm.getTestResult()));
55  		    	addRow(realm.getAccount().getUser(), getAuth(realm), realm.getDomain(), realm.getTestHost(), status);
56              }
57          }
58      }
59  /*	
60  	private StatusImage getStatusImage(KeyStore keys, Realm realm) {
61  	    // Missing authentication
62  		if (missingAuth(realm)) 
63  	    	return StatusImage.ERROR; 
64  
65  		// No host to test against
66  	    if (!realm.acceptsScheme(ProtocolScheme.SSH) || realm.getTestHost() == null || "".equals(realm.getTestHost())) 
67  	    	return StatusImage.HELP;  
68      	
69          try {
70              keys.createHostLink(ProtocolScheme.SSH.toURIStr(realm.getTestHost())).createHandle().close();
71          	// Authentication worked at least with test host
72          	return StatusImage.OK;
73          } catch (HandleCreationException e) {
74          	// Authentication failed with test host - could work with other hosts though
75      	    return StatusImage.WARN;
76          } catch (HandleOperationException e) {
77          	// Closing handle failed with test host - could work with other hosts though
78      	    return StatusImage.WARN;
79          }
80      }
81  */
82  	private String getAuth(Realm realm) {
83  		return missingAuth(realm) ? "< MISSING >" : "***********";
84      }
85  
86  	private boolean missingAuth(Realm realm) {
87  	    return realm.getAccount().missingPasskey();
88      }
89  }
90