You are on page 1of 7

package com.placester.sites.api.

servlet;

import com.placester.notification.api.client.NotificationClient;
import com.placester.sites.api.servlet.util.GoogleMapsUtil;
import com.placester.sites.data.store.db.DomainMappingDB;
import com.placester.sites.data.store.db.OptionDB;
import com.placester.sites.data.store.db.PLSiteDBUtil;
import com.placester.sites.data.store.db.WordPressDBFactory;
import com.placester.sites.data.store.db.dao.APIKey;
import com.placester.sites.data.store.db.dao.DaoUtil;
import com.placester.sites.server.ErrorCode;
import com.placester.sites.server.ErrorCode.StatusCode;
import com.placester.sites.server.SitesConfig;
import com.placester.wordpress.dao.DomainMapping;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

public class DomainServlet extends InternalRESTServlet {

private static final long serialVersionUID = -5280918383822815163L;

public static final String VERSION = "v1";


public static final String PATH = "/domain";
private final PLSiteDBUtil plSiteDBUtil;
private final WordPressDBFactory<OptionDB> optionDbFactory;
private final WordPressDBFactory<DomainMappingDB> domainMappingDbFactory;

public DomainServlet(SitesConfig config, PLSiteDBUtil plSiteDBUtil,


WordPressDBFactory<OptionDB> optionDbFactory, WordPressDBFactory<DomainMappingDB>
domainMappingDbFactory) {
super(config);
this.plSiteDBUtil = plSiteDBUtil;
this.optionDbFactory = optionDbFactory;
this.domainMappingDbFactory = domainMappingDbFactory;
}

@Override
public String getPath() {
return PATH;
}

protected static final com.placester.plsite.dao.Site REF = new


com.placester.plsite.dao.Site();
@Override
protected void retrieve(HttpServletRequest req, HttpServletResponse res,
APIKey creds) throws Exception {
// Parse query string...
JSONObject params = parseQueryString(req);

int blog_id = params.optInt(DomainMapping.BLOG_ID, 0);


boolean active = params.optBoolean(DomainMapping.ACTIVE, false);
String siteid = params.optString(com.placester.plsite.dao.Site.SITEID,
null);
com.placester.plsite.dao.Site s = null;

if (blog_id == 0) {
if (siteid != null) {
List<com.placester.plsite.dao.Site> list =
plSiteDBUtil.get(REF, new String[]{siteid});
if (list != null && !list.isEmpty()) {
s = list.get(0);
try {
blog_id = s.external_site_id != null ?
Integer.parseInt(s.external_site_id) : 0;
} catch (Exception e) {
}
}
}
}else {
List<com.placester.plsite.dao.Site> list =
plSiteDBUtil.getSites(new String[]{String.valueOf(blog_id)},
com.placester.plsite.dao.Site.EXTERNAL_SITE_ID, -1,
-1, -1);

if (list != null && !list.isEmpty()) {


s = list.get(0);
}

DomainMappingDB domain_db = domainMappingDbFactory.fromBlogID(blog_id);


JSONObject payload = null;

if (blog_id > 0 && active) {


// Fetch the primary domain that is mapped...
String primary_domain = domain_db.getPrimaryDomain();
payload = new JSONObject().put(DomainMapping.DOMAIN,
primary_domain);

}else if (blog_id > 0) {


// Retrieve existing domain mappings...
List<DomainMapping> mappings = domain_db.getMappings();
payload = new JSONObject().put(DomainMapping.ENTITIES,
DaoUtil.listToJSON(mappings)).put(COUNT, domain_db.getFoundRows());

if (siteid != null)
payload.put(com.placester.plsite.dao.Site.SITEID, siteid);
}else{
sendError(res, ErrorCode.PARAM_MISSING);
return;
}

sendOkResponse(res, payload);
}

@Override
protected void create(HttpServletRequest req, HttpServletResponse res, APIKey
creds) throws Exception {
// Parse request body...
JSONObject reqBody = parseRequestBody(req);

int blog_id = reqBody.optInt(DomainMapping.BLOG_ID, 0);


String domain = reqBody.optString(DomainMapping.DOMAIN, null);

boolean siteExistsForDomain = false;

com.placester.plsite.dao.Site s = null;
String id = reqBody.optString(com.placester.plsite.dao.Site.SITEID,
null);

if (blog_id == 0) {

if (id != null) {
List<com.placester.plsite.dao.Site> list =
plSiteDBUtil.get(REF, new String[]{id});

if (list != null && !list.isEmpty()) {


s = list.get(0);
try {
blog_id = s.external_site_id != null ?
Integer.parseInt(s.external_site_id) : 0;
} catch (Exception e) {}
}
}

}else{
List<com.placester.plsite.dao.Site> list = plSiteDBUtil.getSites(
new String[]{String.valueOf(blog_id)},
com.placester.plsite.dao.Site.EXTERNAL_SITE_ID, -1,
-1, -1);

if (list != null && !list.isEmpty()) {


s = list.get(0);
}
}

siteExistsForDomain =
plSiteDBUtil.sitesExistForDomain(ServletUtil.getPLSiteDB(config), domain,
((SitesConfig) (this.config)).is_staging);

if (blog_id > 0 && StringUtils.isNotBlank(domain) && !


siteExistsForDomain) {
DomainMappingDB domain_db =
domainMappingDbFactory.fromBlogID(blog_id);

DomainMapping mapping = domain_db.createMapping(reqBody);


if (mapping == null) {
sendError(res, new ErrorCode(StatusCode.SERVER_ERROR,
"Error creating domain mapping in the DB"));
return;
}

NotificationClient notification = ((SitesConfig)


config).notificationClient;

if (notification != null) notification.createDomain(reqBody);

// New domains require a Google Maps API key


GoogleMapsUtil.setAPIKey(optionDbFactory.fromBlogID(blog_id));

if (s != null) {
com.placester.plsite.dao.DomainMapping d = new
com.placester.plsite.dao.DomainMapping();
d.id = UUID.randomUUID().toString();
d.site_id = s.id;
d.domain = domain;
d.primary = mapping.isActive();
d.setCreatedUpdated();
plSiteDBUtil.create(d);
}
}else {
if(siteExistsForDomain) {
sendError(res, ErrorCode.ALREADY_EXISTS);
return;
}

else {
sendError(res, ErrorCode.PARAM_MISSING);
return;
}

sendOkResponse(res, new JSONObject());


}

@Override
protected void update(HttpServletRequest req, HttpServletResponse res, APIKey
creds) throws Exception {
// Parse request body...
JSONObject reqBody = parseRequestBody(req);

int blog_id = reqBody.optInt(DomainMapping.BLOG_ID, 0);


String domain = reqBody.optString(DomainMapping.DOMAIN, null);
com.placester.plsite.dao.Site s = null;
String id = reqBody.optString(com.placester.plsite.dao.Site.SITEID,
null);
boolean siteExistsForDomain = false;

if (blog_id == 0) {

if (id != null) {
List<com.placester.plsite.dao.Site> list =
plSiteDBUtil.get(REF, new String[]{id});
if (list != null && !list.isEmpty()) {
s = list.get(0);
try {
blog_id = s.external_site_id != null ?
Integer.parseInt(s.external_site_id) : 0;
} catch (Exception e) {}
}
}

}else {
List<com.placester.plsite.dao.Site> list =
plSiteDBUtil.getSites(new String[]{String.valueOf(blog_id)},
com.placester.plsite.dao.Site.EXTERNAL_SITE_ID, -1, -1,
-1);

if (list != null && !list.isEmpty()) {


s = list.get(0);
}

siteExistsForDomain =
plSiteDBUtil.sitesExistForDomain(ServletUtil.getPLSiteDB(config), domain,
((SitesConfig) (this.config)).is_staging);

if (blog_id > 0 && StringUtils.isNotBlank(domain) && !


siteExistsForDomain) {
DomainMappingDB domain_db =
domainMappingDbFactory.fromBlogID(blog_id);
if (domain_db.setPrimaryMapping(domain) <= 0) {
sendError(res, ErrorCode.GENERIC_ERROR);
return;
}

NotificationClient notification = ((SitesConfig)


config).notificationClient;

if (notification != null) notification.updateDomain(reqBody);

// When switching domains make sure they have a key


GoogleMapsUtil.setAPIKey(optionDbFactory.fromBlogID(blog_id));

if (s != null) plSiteDBUtil.setPrimaryDomain(s.id, domain);

}else{
if(siteExistsForDomain) {
sendError(res, ErrorCode.ALREADY_EXISTS);
return;
}
else {
sendError(res, ErrorCode.PARAM_MISSING);
return;
}
}

sendOkResponse(res, new JSONObject());


}
@Override
protected void delete(HttpServletRequest req, HttpServletResponse res, APIKey
creds) throws Exception {
// Parse request body...
JSONObject reqBody = parseRequestBody(req);

int blog_id = reqBody.optInt(DomainMapping.BLOG_ID, 0);


String domain = reqBody.optString(DomainMapping.DOMAIN, null);
com.placester.plsite.dao.Site s = null;

String siteid = null;


siteid = reqBody.optString(com.placester.plsite.dao.Site.SITEID, null);

if (blog_id == 0){

if (siteid != null) {
List<com.placester.plsite.dao.Site> list =
plSiteDBUtil.get(REF, new String[]{siteid});
if (list != null && !list.isEmpty()) {
s = list.get(0);

try {
blog_id = s.external_site_id != null ?
Integer.parseInt(s.external_site_id) : 0;
} catch (Exception e) {}

reqBody.put(DomainMapping.BLOG_ID, blog_id);
}
}else{
List<com.placester.plsite.dao.Site> list =
plSiteDBUtil.getSites(new String[]{String.valueOf(blog_id)},
com.placester.plsite.dao.Site.EXTERNAL_SITE_ID, -1,
-1, -1);

if (list != null && !list.isEmpty()) {


s = list.get(0);
}

if (s != null) {plSiteDBUtil.deleteDomainFromSite(s.id, domain);}

NotificationClient notification = ((SitesConfig)


config).notificationClient;

if (notification != null) notification.deleteDomain(reqBody);

DomainMappingDB domain_db = domainMappingDbFactory.fromBlogID(blog_id);

if (blog_id > 0 && StringUtils.isNotBlank(domain)) {


// Delete single mapping...
if (domain_db.deleteMapping(reqBody) <= 0) {
sendError(res, ErrorCode.GENERIC_ERROR);
return;
}
}else if (blog_id > 0) {
// Delete all mappings for the given blog...
if (domain_db.deleteAllMappings() <= 0){
sendError(res, ErrorCode.GENERIC_ERROR);
return;
}

}else{
sendError(res, ErrorCode.PARAM_MISSING);
return;
}

sendOkResponse(res, new JSONObject());


}
}

You might also like