Created
January 5, 2017 13:36
-
-
Save cfalzone/e80fc4cb64fd8c8a53e89b0247ff746a to your computer and use it in GitHub Desktop.
Rest API for dotCMS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.aquent.osgi; | |
import com.aquent.AquentResource; | |
import com.dotcms.repackage.org.apache.felix.http.api.ExtHttpService; | |
import com.dotcms.repackage.org.osgi.framework.BundleContext; | |
import com.dotcms.repackage.org.osgi.framework.ServiceReference; | |
import com.dotcms.repackage.org.osgi.util.tracker.ServiceTracker; | |
import com.dotcms.repackage.org.tuckey.web.filters.urlrewrite.Condition; | |
import com.dotcms.repackage.org.tuckey.web.filters.urlrewrite.NormalRule; | |
import com.dotcms.repackage.org.tuckey.web.filters.urlrewrite.Rule; | |
import com.dotcms.repackage.org.tuckey.web.filters.urlrewrite.SetAttribute; | |
import com.dotcms.rest.config.RestServiceUtil; | |
import com.dotmarketing.filters.DotUrlRewriteFilter; | |
import com.dotmarketing.util.Logger; | |
/** | |
* Activates our plugin. | |
* | |
* @author cfalzone | |
*/ | |
public class AquentActivator extends GenericBundleActivator { | |
/** | |
* Starts the plugin. | |
* | |
* @param ctx The BunbleContext | |
* @throws Exception from initializeServices | |
*/ | |
@Override | |
public void start(BundleContext ctx) throws Exception { | |
Logger.info(this, "Starting Main OSGI Plugin"); | |
initializeServices(ctx); | |
publishBundleServices(ctx); | |
// Add rest services | |
Logger.info(this, "Adding REST Services"); | |
RestServiceUtil.addResource(AquentResource.class); | |
Logger.info(this, "Finished Main OSGI Plugin Startup"); | |
} | |
/** | |
* Stops the plugin. | |
* | |
* @param ctx The bundle Context | |
* @throws Exception from unregisterServices | |
*/ | |
@Override | |
public void stop(BundleContext ctx) throws Exception { | |
RestServiceUtil.removeResource(AquentResource.class); | |
unpublishBundleServices(); | |
unregisterServices(ctx); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.aquent; | |
import javax.servlet.http.HttpServletRequest; | |
import com.dotcms.repackage.javax.ws.rs.GET; | |
import com.dotcms.repackage.javax.ws.rs.POST; | |
import com.dotcms.repackage.javax.ws.rs.PUT; | |
import com.dotcms.repackage.javax.ws.rs.Path; | |
import com.dotcms.repackage.javax.ws.rs.PathParam; | |
import com.dotcms.repackage.javax.ws.rs.Produces; | |
import com.dotcms.repackage.javax.ws.rs.core.CacheControl; | |
import com.dotcms.repackage.javax.ws.rs.core.Context; | |
import com.dotcms.repackage.javax.ws.rs.core.MediaType; | |
import com.dotcms.repackage.javax.ws.rs.core.Response; | |
import com.dotcms.repackage.javax.ws.rs.core.Response.ResponseBuilder; | |
import com.dotcms.rest.InitDataObject; | |
import com.dotcms.rest.WebResource; | |
import com.dotmarketing.business.APILocator; | |
import com.dotmarketing.exception.DotDataException; | |
import com.dotmarketing.exception.DotSecurityException; | |
import com.dotmarketing.portlets.contentlet.business.ContentletAPI; | |
import com.dotmarketing.portlets.contentlet.business.DotContentletStateException; | |
import com.dotmarketing.portlets.contentlet.model.Contentlet; | |
import com.dotmarketing.util.Logger; | |
import com.liferay.portal.model.User; | |
/** | |
* This is a jersey rest api used for deleting files. | |
* | |
* @author cfalzone | |
* | |
*/ | |
@Path("/aqresource") | |
public class AquentResource { | |
private final WebResource webResource = new WebResource(); | |
private ContentletAPI conAPI = APILocator.getContentletAPI(); | |
private static long lang = APILocator.getLanguageAPI().getDefaultLanguage().getId(); | |
/** | |
* Handles PUT requests for delete-id. | |
* @param request - The request object | |
* @param identifier - The identifier to delete | |
* @return Jersy Response | |
* @throws DotContentletStateException from delete | |
* @throws DotDataException from delete | |
* @throws DotSecurityException from delete | |
*/ | |
@PUT | |
@Path("/delete-id/{identifier}") | |
@Produces(MediaType.TEXT_PLAIN) | |
public Response deletePut(@Context HttpServletRequest request, @PathParam("identifier") String identifier) | |
throws DotContentletStateException, DotDataException, DotSecurityException { | |
return delete(request, identifier); | |
} | |
/** | |
* Handles POST requests for delete-id. | |
* @param request - The request object | |
* @param identifier - The identifier to delete | |
* @return Jersy Response | |
* @throws DotContentletStateException from delete | |
* @throws DotDataException from delete | |
* @throws DotSecurityException from delete | |
*/ | |
@POST | |
@Path("/delete-id/{identifier}") | |
@Produces(MediaType.TEXT_PLAIN) | |
public Response deletePost(@Context HttpServletRequest request, @PathParam("identifier") String identifier) | |
throws DotContentletStateException, DotDataException, DotSecurityException { | |
return delete(request, identifier); | |
} | |
/** | |
* Handles GET requests for delete-id. | |
* @param request - The request object | |
* @param identifier - The identifier to delete | |
* @return Jersy Response | |
* @throws DotContentletStateException from delete | |
* @throws DotDataException from delete | |
* @throws DotSecurityException from delete | |
*/ | |
@GET | |
@Path("/delete-id/{identifier}") | |
@Produces(MediaType.TEXT_PLAIN) | |
public Response deleteGet(@Context HttpServletRequest request, @PathParam("identifier") String identifier) | |
throws DotContentletStateException, DotDataException, DotSecurityException { | |
return delete(request, identifier); | |
} | |
/** | |
* Deletes a contentlet by identifier. | |
* @param request - The request object | |
* @param identifier - The identifier to delete | |
* @return The Jersey Repsonse | |
* @throws DotContentletStateException from conAPI | |
* @throws DotDataException from conAPI | |
* @throws DotSecurityException from conAPI | |
*/ | |
private Response delete(HttpServletRequest request, String identifier) | |
throws DotContentletStateException, DotDataException, DotSecurityException { | |
InitDataObject auth = webResource.init(true, request, false); | |
User user = auth.getUser(); | |
if (user != null) { | |
Logger.info(this, "User:" + user.getEmailAddress() + " deleting content with identifier:" + identifier); | |
// First Find the contentlet | |
Contentlet con = conAPI.findContentletByIdentifier(identifier, true, lang, user, true); | |
if (con == null || con.getIdentifier() == null) { | |
// Couldn't find the content | |
Logger.warn(this, "Contentlet not found with identifier:" + identifier); | |
CacheControl cc = new CacheControl(); | |
cc.setNoCache(true); | |
ResponseBuilder builder = Response.ok("FAIL:NoContent", MediaType.TEXT_PLAIN); | |
return builder.cacheControl(cc).build(); | |
} | |
// unpublish | |
conAPI.unpublish(con, user, true); | |
// archive | |
conAPI.archive(con, user, true); | |
// delete | |
conAPI.delete(con, user, true); | |
// Return the response | |
CacheControl cc = new CacheControl(); | |
cc.setNoCache(true); | |
ResponseBuilder builder = Response.ok("SUCCESS", MediaType.TEXT_PLAIN); | |
return builder.cacheControl(cc).build(); | |
} else { | |
// No User | |
Logger.warn(this, "Unaotherized user attempted to delete contentlet with identifier:" + identifier); | |
CacheControl cc = new CacheControl(); | |
cc.setNoCache(true); | |
ResponseBuilder builder = Response.ok("FAIL:NoUser", MediaType.TEXT_PLAIN); | |
return builder.cacheControl(cc).build(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment