View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
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 de.smartics.properties.admin.resources.representation.html;
17  
18  import java.io.OutputStream;
19  import java.util.List;
20  
21  import de.smartics.html5.jatl.HtmlResourceContext;
22  import de.smartics.properties.admin.domain.model.JndiConfiguration;
23  import de.smartics.properties.admin.domain.model.Paths;
24  import de.smartics.properties.admin.resources.representation.html.share.AbstractHtmlRepresentationRenderer;
25  import de.smartics.properties.api.core.domain.PropertiesContext;
26  import de.smartics.resteasy.hypermedia.renderer.LinkDescriptor;
27  
28  /**
29   * Displays an HTML form configure application relevant properties.
30   */
31  public final class JndiHtmlRepresentationRenderer extends
32      AbstractHtmlRepresentationRenderer<JndiConfiguration>
33  {
34    // ********************************* Fields *********************************
35  
36    // --- constants ------------------------------------------------------------
37  
38    /**
39     * The title to the page.
40     */
41    private static final String TITLE = "JNDI Tool";
42  
43    // --- members --------------------------------------------------------------
44  
45    /**
46     * The domain object to render.
47     */
48    private final JndiConfiguration domainObject;
49  
50    // ****************************** Initializer *******************************
51  
52    // ****************************** Constructors ******************************
53  
54    /**
55     * Default constructor.
56     *
57     * @param htmlContext the context information to render the representation.
58     * @param domainObject the domain object to render.
59     * @param entityStream the stream to write to.
60     */
61    public JndiHtmlRepresentationRenderer(final HtmlResourceContext htmlContext,
62        final JndiConfiguration domainObject, final OutputStream entityStream)
63    {
64      super(htmlContext, entityStream);
65      this.domainObject = domainObject;
66    }
67  
68    // ****************************** Inner Classes *****************************
69  
70    // ********************************* Methods ********************************
71  
72    // --- init -----------------------------------------------------------------
73  
74    // --- get&set --------------------------------------------------------------
75  
76    // --- business -------------------------------------------------------------
77  
78    @Override
79    protected void bodyContents()
80    {
81      renderPageTitle(">>>");
82      html.p()
83          .text(
84              "This form allows to configure properties relevant to the application.")
85          .end();
86  
87      html.div().classAttr("alert alert-info");
88      html.button().type("button").classAttr("close")
89          .attr("data-dismiss", "alert").raw("&times;").end();
90      html.h4().text("Defaults to Classpath").end();
91      html.strong().text("Info: ").end();
92      html.text("Please note that JNDI properties are only used for properties"
93                + " that are not configured via classpath properties in" + " "
94                + PropertiesContext.BOOT_PROPERTIES_HOME + "!");
95      html.end();
96  
97      renderForm();
98    }
99  
100   private void renderForm()
101   {
102     html.form().method("POST").classAttr("form-horizontal");
103 
104     renderApplication();
105     renderDataSource();
106     renderCache();
107     renderSecurity();
108 
109     createSubmitButton();
110 
111     html.end();
112   }
113 
114   private void renderApplication()
115   {
116     html.fieldset().legend().text("Application").end();
117     renderInputField(Paths.PARAM_JNDI_APP_GROUP_ID, "Group ID",
118         domainObject.getGroupId(), "org.mycorp.myapp");
119     renderInputField(Paths.PARAM_JNDI_APP_ARTIFACT_ID, "Artifact ID",
120         domainObject.getArtifactId(), "test-myapp");
121     renderInputField(Paths.PARAM_JNDI_APP_VERSION, "Version",
122         domainObject.getVersion(), "3.23.42");
123     renderInputField(Paths.PARAM_JNDI_APP_ARCHIVE_TYPE, "Archive Type",
124         domainObject.getArchiveType(), "pom");
125     renderInputField(Paths.PARAM_JNDI_APP_CLASSIFIER, "Classifier",
126         domainObject.getClassifier(), "anything");
127     html.end();
128   }
129 
130   private void renderDataSource()
131   {
132     html.fieldset().legend().text("Data Source").end();
133     renderInputField(Paths.PARAM_JNDI_DS_MAPPED_NAME, "Mapped Name",
134         domainObject.getDsName(), "java:jboss/datasources/propertiesDS");
135     renderInputField(Paths.PARAM_JNDI_DS_CREATE_TABLE, "Create Table",
136         domainObject.getDsCreateTable(), true);
137     renderInputField(Paths.PARAM_JNDI_DS_DROP_TABLE, "Drop Table",
138         domainObject.getDsDropTable(), false);
139     html.end();
140   }
141 
142   private void renderCache()
143   {
144     html.fieldset().legend().text("Cache").end();
145     renderInputField(Paths.PARAM_JNDI_CACHE_MAPPED_NAME, "Mapped Name",
146         domainObject.getCacheName(),
147         "java:jboss/infinispan/container/smartics-properties");
148     html.end();
149   }
150 
151   private void renderSecurity()
152   {
153     html.fieldset().legend().text("Security").end();
154     renderInputField(Paths.PARAM_JNDI_SECURITY_KEY, "Key",
155         domainObject.getSecurityKey(), "+pvrmeQCmtWmYVOZ57uuIQ==");
156     renderInputField(Paths.PARAM_JNDI_SECURITY_ALGORITHM, "Algorithm",
157         domainObject.getSecurityAlgorithm(), "AES");
158     renderInputField(Paths.PARAM_JNDI_SECURITY_PROVIDER, "Provider",
159         domainObject.getSecurityProvider(), "...");
160     renderInputField(Paths.PARAM_JNDI_SECURITY_TRANSFORMATION,
161         "Transformation", domainObject.getSecurityTransformation(),
162         "AES/ECB/PKCS5Padding");
163     html.end();
164   }
165 
166   private void renderInputField(final String id, final String label,
167       final Boolean value, final boolean defaultValue)
168   {
169     html.div().classAttr("control-group");
170 
171     html.label().classAttr("control-label").forAttr(id).text(label).end();
172 
173     html.div().classAttr("controls");
174 
175     final boolean checked = value != null ? value : defaultValue;
176     html.input().type("checkbox").id(id).name(id);
177     if (checked)
178     {
179       html.checked("checked");
180     }
181 
182     html.end(3);
183   }
184 
185   private void renderInputField(final String id, final String label,
186       final String value, final String placeholder)
187   {
188     html.div().classAttr("control-group");
189 
190     html.label().classAttr("control-label").forAttr(id).text(label).end();
191 
192     html.div().classAttr("controls");
193     html.input().type("text").id(id).name(id).classAttr("input-xlarge")
194         .value(value);
195     html.attr("placeholder", placeholder);
196     html.end(3);
197   }
198 
199   private void createSubmitButton()
200   {
201     html.button().type("submit").id("submit").classAttr("btn btn-default");
202     html.text("Submit").end();
203   }
204 
205   @Override
206   protected String getTitle()
207   {
208     return TITLE;
209   }
210 
211   @Override
212   protected List<LinkDescriptor> breadcrumbLinks()
213   {
214     return breadcrumb.jndi();
215   }
216 
217   // --- object basics --------------------------------------------------------
218 
219 }