public void get(HttpServletRequest request, HttpServletResponse response) throws Exception { String redirectFrom = StringUtils.lowerCase( ServletRequestUtils.getRequiredStringParameter(request, "redirectFrom")); Redirection redirection = redirectionDao.getRedirection(redirectFrom); if (redirection == null) { response.sendError( HttpServletResponse.SC_BAD_REQUEST, "Web address " + redirectFrom + ".subsonic.org not registered."); return; } PrintWriter writer = response.getWriter(); String url = redirection.getRedirectTo(); if (!url.endsWith("/")) { url += "/"; } writer.println(url); url = redirection.getLocalRedirectTo(); if (!url.endsWith("/")) { url += "/"; } writer.println(url); }
public void unregister(HttpServletRequest request, HttpServletResponse response) throws Exception { String serverId = ServletRequestUtils.getStringParameter(request, "serverId"); if (!StringUtils.isEmpty(serverId)) { redirectionDao.deleteRedirectionsByServerId(serverId); } }
public void dump(HttpServletRequest request, HttpServletResponse response) throws Exception { File file = File.createTempFile("redirections", ".txt"); PrintWriter writer = new PrintWriter(file, "UTF-8"); try { int offset = 0; int count = 100; while (true) { List<Redirection> redirections = redirectionDao.getAllRedirections(offset, count); if (redirections.isEmpty()) { break; } offset += redirections.size(); for (Redirection redirection : redirections) { writer.println(redirection); } } LOG.info("Dumped redirections to " + file.getAbsolutePath()); } finally { IOUtils.closeQuietly(writer); } }
public void register(HttpServletRequest request, HttpServletResponse response) throws Exception { String redirectFrom = StringUtils.lowerCase( ServletRequestUtils.getRequiredStringParameter(request, "redirectFrom")); String licenseHolder = StringUtils.lowerCase( StringUtils.trimToNull( ServletRequestUtils.getStringParameter(request, "licenseHolder"))); String serverId = ServletRequestUtils.getRequiredStringParameter(request, "serverId"); String host = ServletRequestUtils.getStringParameter(request, "host", request.getRemoteAddr()); int port = ServletRequestUtils.getRequiredIntParameter(request, "port"); Integer localPort = ServletRequestUtils.getIntParameter(request, "localPort"); String localIp = ServletRequestUtils.getStringParameter(request, "localIp"); String contextPath = ServletRequestUtils.getRequiredStringParameter(request, "contextPath"); boolean trial = ServletRequestUtils.getBooleanParameter(request, "trial", false); Date now = new Date(); Date trialExpires = null; if (trial) { trialExpires = new Date(ServletRequestUtils.getRequiredLongParameter(request, "trialExpires")); } else if (licenseHolder == null) { sendError(response, "Invalid license."); } if (RESERVED_REDIRECTS.containsKey(redirectFrom)) { sendError(response, "\"" + redirectFrom + "\" is a reserved address. Please select another."); return; } if (!redirectFrom.matches("(\\w|\\-)+")) { sendError( response, "Illegal characters present in \"" + redirectFrom + "\". Please select another."); return; } URL url = new URL("http", host, port, "/" + contextPath); String redirectTo = url.toExternalForm(); String localRedirectTo = null; if (localIp != null && localPort != null) { URL localUrl = new URL("http", localIp, localPort, "/" + contextPath); localRedirectTo = localUrl.toExternalForm(); } Redirection redirection = redirectionDao.getRedirection(redirectFrom); if (redirection == null) { // Delete other redirects for same server ID. redirectionDao.deleteRedirectionsByServerId(serverId); redirection = new Redirection( 0, licenseHolder, serverId, redirectFrom, redirectTo, localRedirectTo, trial, trialExpires, now, null, 0); redirectionDao.createRedirection(redirection); LOG.info("Created " + redirection); } else { boolean sameServerId = serverId.equals(redirection.getServerId()); boolean sameLicenseHolder = licenseHolder != null && licenseHolder.equalsIgnoreCase(redirection.getLicenseHolder()); // Note: A licensed user can take over any expired trial domain. boolean existingTrialExpired = redirection.getTrialExpires() != null && redirection.getTrialExpires().before(now); if (sameServerId || sameLicenseHolder || (existingTrialExpired && !trial)) { redirection.setLicenseHolder(licenseHolder); redirection.setServerId(serverId); redirection.setRedirectFrom(redirectFrom); redirection.setRedirectTo(redirectTo); redirection.setLocalRedirectTo(localRedirectTo); redirection.setTrial(trial); redirection.setTrialExpires(trialExpires); redirection.setLastUpdated(now); redirectionDao.updateRedirection(redirection); LOG.info("Updated " + redirection); } else { sendError( response, "The web address \"" + redirectFrom + "\" is already in use. Please select another."); } } }
public void test(HttpServletRequest request, HttpServletResponse response) throws Exception { String redirectFrom = StringUtils.lowerCase(ServletRequestUtils.getStringParameter(request, "redirectFrom")); String customUrl = ServletRequestUtils.getStringParameter(request, "customUrl"); PrintWriter writer = response.getWriter(); String webAddress; String url; if (redirectFrom != null) { Redirection redirection = redirectionDao.getRedirection(redirectFrom); webAddress = redirectFrom + ".subsonic.org"; if (redirection == null) { writer.print("Web address " + webAddress + " not registered."); return; } if (redirection.getTrialExpires() != null && redirection.getTrialExpires().before(new Date())) { writer.print( "Trial period expired. Please upgrade to Subsonic Premium to activate web address."); return; } url = redirection.getRedirectTo(); } else { webAddress = customUrl; url = customUrl; } if (!url.endsWith("/")) { url += "/"; } url += "crossdomain.xml"; HttpClient client = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000); HttpConnectionParams.setSoTimeout(client.getParams(), 15000); HttpGet method = new HttpGet(url); try { HttpResponse resp = client.execute(method); StatusLine status = resp.getStatusLine(); if (status.getStatusCode() == HttpStatus.SC_OK) { String content = IOUtils.toString(resp.getEntity().getContent()); if (!content.contains("cross-domain-policy")) { String msg = webAddress + " responded, but with unexpected content."; writer.print(msg); LOG.info(msg); } else { String msg = webAddress + " responded successfully."; writer.print(msg); LOG.info(msg); } } else { String msg = webAddress + " returned HTTP error code " + status.getStatusCode() + " " + status.getReasonPhrase(); writer.print(msg); LOG.info(msg); } } catch (SSLPeerUnverifiedException x) { String msg = webAddress + " responded successfully, but could not authenticate it."; writer.print(msg); LOG.info(msg); } catch (Throwable x) { String msg = "Could not connect to " + webAddress + ". (" + x.getClass().getSimpleName() + ")"; writer.print(msg); LOG.info(msg); } finally { client.getConnectionManager().shutdown(); } }