comparison src/nabble/view/web/forum/ChangeDomainName.jtp @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children 18cf4872fd7f
comparison
equal deleted inserted replaced
-1:000000000000 0:7ecd1a4ef557
1 <%
2 package nabble.view.web.forum;
3
4 import fschmidt.util.mail.Mail;
5 import fschmidt.util.mail.MailAddress;
6 import fschmidt.util.mail.MailException;
7 import fschmidt.util.mail.MailHome;
8 import fschmidt.util.mail.PlainTextContent;
9 import nabble.model.ModelException;
10 import nabble.model.ModelHome;
11 import nabble.model.Node;
12 import nabble.model.Site;
13 import nabble.model.User;
14 import nabble.naml.namespaces.TemplateException;
15 import nabble.view.lib.Jtp;
16 import nabble.view.lib.Permissions;
17 import nabble.view.lib.Shared;
18
19 import javax.servlet.ServletException;
20 import javax.servlet.http.HttpServlet;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23 import java.io.IOException;
24 import java.io.PrintWriter;
25 import java.net.InetAddress;
26 import java.net.UnknownHostException;
27
28
29 public final class ChangeDomainName extends HttpServlet {
30
31 protected void service(HttpServletRequest request,HttpServletResponse response)
32 throws ServletException, IOException
33 {
34 Site site = ModelHome.getSite(Jtp.getLong(request, "site"));
35 if (site == null) {
36 response.sendError(HttpServletResponse.SC_NOT_FOUND, "No application match the \"<i>" + request.getHeader("host") + "</i>\" domain.");
37 return;
38 }
39
40 User user = Jtp.getUser(request, response);
41 if (user == null) {
42 response.sendRedirect("http://" + Jtp.getDefaultBaseUrl(site) + Jtp.loginPath(site,null,response.encodeURL("/forum/ChangeDomainName.jtp?site="+site.getId())) );
43 return;
44 }
45
46 boolean isSiteAdmin = Permissions.isInGroup(user, Permissions.ADMINISTRATORS_GROUP);
47 boolean isSysAdmin = Permissions.isSysAdmin(user);
48 if (!isSiteAdmin && !isSysAdmin) {
49 Jtp.login("Only administrators can proceed in this area.", request, response);
50 return;
51 }
52
53 Node rootNode = site.getRootNode();
54 boolean isSave = "save".equals(request.getParameter("action"));
55
56 String domainName = site.getCustomDomain();
57 String domainOption = domainName == null? "default-domain" : "own-domain";
58 if (isSave) {
59 domainName = request.getParameter("domain-name");
60 domainOption = request.getParameter("domain-option");
61 }
62
63 String errorMsg = null;
64 if (isSave && "POST".equals(request.getMethod())) {
65 try {
66 boolean isDefault = "default-domain".equals(domainOption);
67 if (isDefault)
68 site.setCustomDomain(null);
69 else {
70 domainName = domainName.trim();
71 if (domainName.length() == 0 || domainName.endsWith(".nabble.com") || domainName.equals(InetAddress.getLocalHost().getHostAddress()))
72 throw ModelException.newInstance("invalid_domain", "Please enter a valid domain name for your application.");
73 Long currentSiteId = ModelHome.getSiteIdFromDomain(domainName);
74 if (currentSiteId != null && !currentSiteId.equals(site.getId()))
75 throw ModelException.newInstance("domain_already_in_use", "This domain is already in use.");
76 String domainIP = getDomainIP(domainName);
77 String nabbleIP = getDomainIP(Jtp.getDefaultHost());
78 if (!nabbleIP.equals(domainIP))
79 throw ModelException.newInstance(
80 "domain_bad_resolution",
81 "Your domain name currently resolves to an IP address that doesn't belong to Nabble. " +
82 "Please follow the instructions (see link below) in order to update your DNS. " +
83 "If you have just updated your DNS, please wait 24 hours for the changes to take affect.");
84
85 site.setCustomDomain(domainName);
86 }
87 sendEmail(user, site);
88 response.sendRedirect(Jtp.url(rootNode));
89 return;
90 } catch (TemplateException e) {
91 errorMsg = e.getMessage();
92 }
93 }
94
95 PrintWriter out = response.getWriter();
96 %>
97 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
98 <html>
99 <head>
100 <META NAME="robots" CONTENT="noindex,nofollow">
101 <% Shared.title(request, response, "Change Domain Name"); %>
102 <style type="text/css">
103 label { font-weight:bold; }
104 .step {
105 font-weight: bold;
106 font-size: 120%;
107 padding: .5em .3em;
108 white-space:nowrap;
109 }
110 </style>
111 <script type="text/javascript">
112 $(document).ready(function() {
113 function enableControls() {
114 if ($('#default-domain').attr('checked')) {
115 $('#domain-name').attr('disabled','y');
116 } else {
117 $('#domain-name').removeAttr('disabled').focus();
118 }
119 };
120 $('#default-domain,#own-domain').change(enableControls);
121 enableControls();
122 });
123 </script>
124 </head>
125 <body>
126 <% Shared.minHeader(request,response, rootNode); %>
127 <% Shared.editHeader(rootNode.getSubjectHtml(), "Change Domain Name", out); %>
128 <% Shared.errorMessage(request, response, errorMsg, null); %>
129
130 <% if (site.getCustomDomain() != null) { %>
131 <div class="info-message rounded" style="padding: .5em;margin:.4em 0">
132 <b>You already have a domain set for "<%=rootNode.getSubject()%>".</b><br/>
133 If you change this domain configuration, links to the old domain will probably stop working.
134 </div>
135 <% } %>
136
137 <div class="field weak-color" style="margin-left:1.5em">
138 <div style="margin:1em 0 .5em">
139 Access your application with a custom domain name.
140 </div>
141
142 <form method="post" action="/forum/ChangeDomainName.jtp" accept-charset="UTF-8">
143 <input type="hidden" name="action" value="save"/>
144 <input type="hidden" name="site" value="<%=site.getId()%>"/>
145 <input type="radio" name="domain-option" id="default-domain" value="default-domain" <%="default-domain".equals(domainOption)?"checked='y'":""%>/>
146 <label for="default-domain">Nabble Default URL</label>
147 <div style="margin:.5em 0 1em 3em">
148 http://<%=Jtp.getDefaultBaseUrl(site)%>/
149 </div>
150
151 <input type="radio" name="domain-option" id="own-domain" value="own-domain" <%="own-domain".equals(domainOption)?"checked='y'":""%>/>
152 <label for="own-domain">Your Own Domain</label>
153 <div style="margin:.5em 0 1em 3em">
154 <table class="weak-color">
155 <tr style="vertical-align:top">
156 <td style="padding-right:.8em"><div class="second-font shaded-bg-color rounded step border1 medium-border-color">Step 1</div></td>
157 <td>
158 Go to your Domain Registrar and set the CNAME-record for your custom domain to <b><%=Jtp.getDefaultHost()%></b>.
159 (<a href="/help/DNSConfiguration.jtp" target="_new">show me more details</a>)
160 <div style="margin-top:.7em;font-size:80%">
161 (After this change, wait a little bit for the DNS to propagate.
162 Then open your custom domain url in the browser to see if the change is propagated &ndash; if successful, you should get redirected to the Nabble home page)
163 </div>
164 </td>
165 </tr>
166 <tr style="vertical-align:top">
167 <td style="padding:1.7em .5em 0 0"><div class="second-font shaded-bg-color rounded step border1 medium-border-color">Step 2</div></td>
168 <td style="padding-top:1.5em">
169 <div style="margin-bottom:.3em">Enter your custom domain name below:</div>
170 http://<input id="domain-name" name="domain-name" size="40" value="<%=Jtp.hideNull(domainName)%>"/>
171 <div style="font-size:80%;margin:.3em 0">
172 Example: mydomain.com
173 </div>
174 </td>
175 </tr>
176 <tr style="vertical-align:top">
177 <td style="padding:1.3em .5em 0 0"><div class="second-font shaded-bg-color rounded step border1 medium-border-color">Step 3</div></td>
178 <td style="padding-top:1.1em">
179 After saving your changes, your old URLs will continue to work and will automatically redirect to your custom domain URL.
180 You will also receive an email with a link to revert this domain change.
181 </td>
182 </tr>
183 </table>
184 </div>
185
186 <input type="submit" value="Save Changes" />
187 or <a href="<%=Jtp.path(rootNode)%>">Cancel</a>
188 </form>
189 </div>
190 <% Shared.footer(request, response); %>
191 <% Shared.analytics(request, response); %>
192 </body>
193 </html>
194 <%
195 }
196
197 private static String getDomainIP(String domain) {
198 String ip = domain;
199 int i = ip.indexOf(":");
200 if( i > 0 )
201 ip = ip.substring(0,i);
202 try {
203 InetAddress add = InetAddress.getByName(ip);
204 if (add != null)
205 ip = add.getHostAddress();
206 } catch(UnknownHostException e) {}
207 return ip;
208 }
209
210 private static void sendEmail(User user, Site site)
211 throws IOException, ServletException
212 {
213 StringBuilder builder = new StringBuilder();
214 builder.append("Dear ").append(user.getName()).append(",\n\n");
215 builder.append("You have changed your domain name configuration to:\n");
216 builder.append(site.getBaseUrl());
217 builder.append("\n\n");
218 builder.append("If you have problems with your domain name or simply want to change this configuration again, you can use this link:\n");
219 builder.append("http://").append(Jtp.getDefaultHost()).append("/forum/ChangeDomainName.jtp?site=").append(site.getId());
220 builder.append("\n\n");
221 builder.append("Regards,\n");
222 builder.append("The Nabble team");
223
224 try {
225 Mail mail = MailHome.newMail();
226 mail.setFrom(new MailAddress(ModelHome.noReply, "Nabble"));
227 mail.setTo(new MailAddress(user.getEmail()));
228 mail.setSubject("Domain name configuration changed");
229 mail.setContent(new PlainTextContent(builder.toString()));
230 ModelHome.send(mail);
231 } catch(MailException e) {
232 throw new RuntimeException(e);
233 }
234 }
235 }
236 %>