0
|
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 out.print( "\n<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n <head>\n <META NAME=\"robots\" CONTENT=\"noindex,nofollow\">\n " );
|
|
98 Shared.title(request, response, "Change Domain Name");
|
|
99 out.print( "\n <style type=\"text/css\">\n label { font-weight:bold; }\n .step {\n font-weight: bold;\n font-size: 120%;\n padding: .5em .3em;\n white-space:nowrap;\n }\n </style>\n <script type=\"text/javascript\">\n $(document).ready(function() {\n function enableControls() {\n if ($('#default-domain').attr('checked')) {\n $('#domain-name').attr('disabled','y');\n } else {\n $('#domain-name').removeAttr('disabled').focus();\n }\n };\n $('#default-domain,#own-domain').change(enableControls);\n enableControls();\n });\n </script>\n </head>\n <body>\n " );
|
|
100 Shared.minHeader(request,response, rootNode);
|
|
101 out.print( "\n " );
|
|
102 Shared.editHeader(rootNode.getSubjectHtml(), "Change Domain Name", out);
|
|
103 out.print( "\n " );
|
|
104 Shared.errorMessage(request, response, errorMsg, null);
|
|
105 out.print( "\n\n " );
|
|
106 if (site.getCustomDomain() != null) {
|
|
107 out.print( "\n <div class=\"info-message rounded\" style=\"padding: .5em;margin:.4em 0\">\n <b>You already have a domain set for \"" );
|
|
108 out.print( (rootNode.getSubject()) );
|
|
109 out.print( "\".</b><br/>\n If you change this domain configuration, links to the old domain will probably stop working.\n </div>\n " );
|
|
110 }
|
|
111 out.print( "\n\n <div class=\"field weak-color\" style=\"margin-left:1.5em\">\n <div style=\"margin:1em 0 .5em\">\n Access your application with a custom domain name.\n </div>\n\n <form method=\"post\" action=\"/forum/ChangeDomainName.jtp\" accept-charset=\"UTF-8\">\n <input type=\"hidden\" name=\"action\" value=\"save\"/>\n <input type=\"hidden\" name=\"site\" value=\"" );
|
|
112 out.print( (site.getId()) );
|
|
113 out.print( "\"/>\n <input type=\"radio\" name=\"domain-option\" id=\"default-domain\" value=\"default-domain\" " );
|
|
114 out.print( ("default-domain".equals(domainOption)?"checked='y'":"") );
|
|
115 out.print( "/>\n <label for=\"default-domain\">Nabble Default URL</label>\n <div style=\"margin:.5em 0 1em 3em\">\n http://" );
|
|
116 out.print( (Jtp.getDefaultBaseUrl(site)) );
|
|
117 out.print( "/\n </div>\n\n <input type=\"radio\" name=\"domain-option\" id=\"own-domain\" value=\"own-domain\" " );
|
|
118 out.print( ("own-domain".equals(domainOption)?"checked='y'":"") );
|
|
119 out.print( "/>\n <label for=\"own-domain\">Your Own Domain</label>\n <div style=\"margin:.5em 0 1em 3em\">\n <table class=\"weak-color\">\n <tr style=\"vertical-align:top\">\n <td style=\"padding-right:.8em\"><div class=\"second-font shaded-bg-color rounded step border1 medium-border-color\">Step 1</div></td>\n <td>\n Go to your Domain Registrar and set the CNAME-record for your custom domain to <b>" );
|
|
120 out.print( (Jtp.getDefaultHost()) );
|
|
121 out.print( "</b>.\n (<a href=\"/help/DNSConfiguration.jtp\" target=\"_new\">show me more details</a>)\n <div style=\"margin-top:.7em;font-size:80%\">\n (After this change, wait a little bit for the DNS to propagate.\n Then open your custom domain url in the browser to see if the change is propagated – if successful, you should get redirected to the Nabble home page)\n </div>\n </td>\n </tr>\n <tr style=\"vertical-align:top\">\n <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>\n <td style=\"padding-top:1.5em\">\n <div style=\"margin-bottom:.3em\">Enter your custom domain name below:</div>\n http://<input id=\"domain-name\" name=\"domain-name\" size=\"40\" value=\"" );
|
|
122 out.print( (Jtp.hideNull(domainName)) );
|
|
123 out.print( "\"/>\n <div style=\"font-size:80%;margin:.3em 0\">\n Example: mydomain.com\n </div>\n </td>\n </tr>\n <tr style=\"vertical-align:top\">\n <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>\n <td style=\"padding-top:1.1em\">\n After saving your changes, your old URLs will continue to work and will automatically redirect to your custom domain URL.\n You will also receive an email with a link to revert this domain change.\n </td>\n </tr>\n </table>\n </div>\n\n <input type=\"submit\" value=\"Save Changes\" />\n or <a href=\"" );
|
|
124 out.print( (Jtp.path(rootNode)) );
|
|
125 out.print( "\">Cancel</a>\n </form>\n </div>\n " );
|
|
126 Shared.footer(request, response);
|
|
127 out.print( "\n " );
|
|
128 Shared.analytics(request, response);
|
|
129 out.print( "\n </body>\n</html>\n" );
|
|
130
|
|
131 }
|
|
132
|
|
133 private static String getDomainIP(String domain) {
|
|
134 String ip = domain;
|
|
135 int i = ip.indexOf(":");
|
|
136 if( i > 0 )
|
|
137 ip = ip.substring(0,i);
|
|
138 try {
|
|
139 InetAddress add = InetAddress.getByName(ip);
|
|
140 if (add != null)
|
|
141 ip = add.getHostAddress();
|
|
142 } catch(UnknownHostException e) {}
|
|
143 return ip;
|
|
144 }
|
|
145
|
|
146 private static void sendEmail(User user, Site site)
|
|
147 throws IOException, ServletException
|
|
148 {
|
|
149 StringBuilder builder = new StringBuilder();
|
|
150 builder.append("Dear ").append(user.getName()).append(",\n\n");
|
|
151 builder.append("You have changed your domain name configuration to:\n");
|
|
152 builder.append(site.getBaseUrl());
|
|
153 builder.append("\n\n");
|
|
154 builder.append("If you have problems with your domain name or simply want to change this configuration again, you can use this link:\n");
|
|
155 builder.append("http://").append(Jtp.getDefaultHost()).append("/forum/ChangeDomainName.jtp?site=").append(site.getId());
|
|
156 builder.append("\n\n");
|
|
157 builder.append("Regards,\n");
|
|
158 builder.append("The Nabble team");
|
|
159
|
|
160 try {
|
|
161 Mail mail = MailHome.newMail();
|
|
162 mail.setFrom(new MailAddress(ModelHome.noReply, "Nabble"));
|
|
163 mail.setTo(new MailAddress(user.getEmail()));
|
|
164 mail.setSubject("Domain name configuration changed");
|
|
165 mail.setContent(new PlainTextContent(builder.toString()));
|
|
166 ModelHome.send(mail);
|
|
167 } catch(MailException e) {
|
|
168 throw new RuntimeException(e);
|
|
169 }
|
|
170 }
|
|
171 }
|
|
172
|