diff --git a/pom.xml b/pom.xml index ae8ce3c..0f2cfcd 100644 --- a/pom.xml +++ b/pom.xml @@ -1,83 +1,86 @@ 4.0.0 org.wikitolearn wiki-rating 0.0.1-SNAPSHOT jar wiki-rating SpringBoot microservice to serve rating functionalities org.springframework.boot spring-boot-starter-parent 1.5.2.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter - + + org.springframework.boot + spring-boot-starter-security + org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web org.wikidata.wdtk wdtk-wikibaseapi 0.7.0 org.json json com.orientechnologies orientdb-core 2.2.17 com.orientechnologies orientdb-graphdb 2.2.17 com.orientechnologies orientdb-client 2.2.17 com.tinkerpop.blueprints blueprints-core 2.6.0 org.springframework.boot spring-boot-maven-plugin diff --git a/src/main/java/org/wikitolearn/WikiRatingApplication.java b/src/main/java/org/wikitolearn/WikiRatingApplication.java index f7b8499..e318933 100644 --- a/src/main/java/org/wikitolearn/WikiRatingApplication.java +++ b/src/main/java/org/wikitolearn/WikiRatingApplication.java @@ -1,14 +1,16 @@ package org.wikitolearn; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; @SpringBootApplication @EnableAsync +@EnableGlobalMethodSecurity(securedEnabled = true) public class WikiRatingApplication { public static void main(String[] args) { SpringApplication.run(WikiRatingApplication.class, args); } } diff --git a/src/main/java/org/wikitolearn/controllers/MaintenanceController.java b/src/main/java/org/wikitolearn/controllers/MaintenanceController.java new file mode 100644 index 0000000..b33d3ae --- /dev/null +++ b/src/main/java/org/wikitolearn/controllers/MaintenanceController.java @@ -0,0 +1,72 @@ +/** + * + */ +package org.wikitolearn.controllers; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.util.Properties; + +import org.json.JSONException; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.security.access.annotation.Secured; +import org.springframework.util.DefaultPropertiesPersister; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author alessandro + * + */ +@RestController +public class MaintenanceController { + + private static final Logger LOG = LoggerFactory.getLogger(MaintenanceController.class); + + @RequestMapping(value = "${maintenance.uri}", method = RequestMethod.POST, produces = "application/json") + @Secured("ROLE_ADMIN") + public String maintenanceMode(@RequestParam(value = "active") String active) { + JSONObject response = new JSONObject(); + int mode = Integer.parseInt(active); + + try { + if (mode == 0) { + response.put("status", "success"); + response.put("message", "Application is live now."); + File f = new File("maintenance.lock"); + if (f.exists()) { + f.delete(); + LOG.info("Deleted maintenance lock file."); + } + LOG.info("Application is live now."); + } else if (mode == 1) { + try { + response.put("status", "success"); + response.put("message", "Application is in maintenance mode now."); + Properties props = new Properties(); + props.setProperty("maintenance.active", "true"); + ; + File lockFile = new File("maintenance.lock"); + OutputStream out = new FileOutputStream(lockFile); + DefaultPropertiesPersister p = new DefaultPropertiesPersister(); + p.store(props, out, "Maintenance mode lock file"); + LOG.info("Application is in maintenance mode now."); + } catch (Exception e) { + LOG.error("Something went wrong.", e.getMessage()); + } + + } else { + response.put("status", "error"); + response.put("message", "Parameter value not supported"); + } + } catch (JSONException e) { + LOG.error("Something went wrong using JSON API.", e.getMessage()); + } + return response.toString(); + } +} diff --git a/src/main/java/org/wikitolearn/dao/PageDAO.java b/src/main/java/org/wikitolearn/dao/PageDAO.java index 3d57b1a..06c3b33 100644 --- a/src/main/java/org/wikitolearn/dao/PageDAO.java +++ b/src/main/java/org/wikitolearn/dao/PageDAO.java @@ -1,101 +1,100 @@ /** * */ package org.wikitolearn.dao; import com.orientechnologies.orient.core.metadata.schema.OClass; import com.orientechnologies.orient.core.metadata.schema.OType; import com.orientechnologies.orient.core.sql.OCommandSQL; import com.orientechnologies.orient.core.storage.ORecordDuplicatedException; -import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import java.util.HashMap; import java.util.List; import java.util.Map; import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; import com.tinkerpop.blueprints.impls.orient.OrientVertex; import com.tinkerpop.blueprints.impls.orient.OrientVertexType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import org.wikitolearn.models.Page; import org.wikitolearn.utils.DbConnection; /** * * This class will handles the operations on DB handling the Pages * @author alessandro * */ @Repository public class PageDAO { private static final Logger LOG = LoggerFactory.getLogger(PageDAO.class); @Autowired private DbConnection connection; /** * This method is used to create the class on the DB. * It creates an unique index on pageid to avoid duplicated. */ public void createDBClass() { LOG.info("Creating DB classes for PageDAO..."); OrientGraphNoTx graph = connection.getDbGraphNT(); try{ OrientVertexType vertex = graph.createVertexType("Page",1); vertex.createProperty("pageid", OType.INTEGER).setMandatory(true); vertex.createProperty("lang", OType.STRING).setMandatory(true); vertex.createIndex("page_lang", OClass.INDEX_TYPE.UNIQUE, "pageid", "lang"); //now we want to add clusters graph.command(new OCommandSQL("ALTER CLASS Page ADDCLUSTER Pages_it")).execute(); graph.command(new OCommandSQL("ALTER CLASS Page ADDCLUSTER Pages_en")).execute(); //adding the clusters to the class Page //graph.getRawGraph().getMetadata().getSchema().reload(); } catch( Exception e ) { LOG.error("Something went wrong during class creation. Operation will be rollbacked.", e.getMessage()); graph.rollback(); } finally { graph.shutdown(); } } /** * Insert all the given pages in the database as vertexes. * If there are duplicates all the insertion is rolled back. * @param pages List The pages to be inserted * @return boolean True if insertion was committed, false otherwise */ public Boolean insertPages(List pages, String lang){ OrientGraph graph = connection.getGraph(); LOG.info("Starting to insert pages..."); try{ for(Page p : pages){ Map props = new HashMap<>(); props.put("pageid", p.getPageid()); props.put( "title", p.getTitle()); props.put("lang", lang); props.put("pageRank", p.getPageRank()); OrientVertex pageNode = graph.addVertex("class:Page,cluster:Pages_"+lang, props); LOG.info("Page inserted " + pageNode.toString()); } graph.commit(); LOG.info("Pages insertion committed"); return true; } catch (ORecordDuplicatedException or) { LOG.error("Some of the pages are duplicates. Operation will be rollbacked.", or.getMessage()); graph.rollback(); } catch( Exception e ) { LOG.error("Something went wrong during page insertion. Operation will be rollbacked.", e.getMessage()); graph.rollback(); }finally { graph.shutdown(); } return false; } } diff --git a/src/main/java/org/wikitolearn/filters/MaintenanceFilter.java b/src/main/java/org/wikitolearn/filters/MaintenanceFilter.java new file mode 100644 index 0000000..4befd07 --- /dev/null +++ b/src/main/java/org/wikitolearn/filters/MaintenanceFilter.java @@ -0,0 +1,74 @@ +/** + * + */ +package org.wikitolearn.filters; + +import java.io.File; +import java.io.IOException; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpMethod; +import org.springframework.stereotype.Component; +/** + * @author alessandro + * + */ +@Component +public class MaintenanceFilter implements Filter { + + private @Value("${maintenance.uri}") String MAINTENANCE_URI; + //private boolean isMaintenanceActive = false; + + /* + * (non-Javadoc) + * + * @see javax.servlet.Filter#destroy() + */ + @Override + public void destroy() { + // TODO Auto-generated method stub + + } + + /* + * (non-Javadoc) + * + * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, + * javax.servlet.ServletResponse, javax.servlet.FilterChain) + */ + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) + throws IOException, ServletException { + HttpServletRequest request = (HttpServletRequest) servletRequest; + HttpServletResponse response = (HttpServletResponse) servletResponse; + File lockFile = new File("maintenance.lock"); + if(MAINTENANCE_URI.equals(request.getRequestURI())){ + filterChain.doFilter(servletRequest, servletResponse); + }else if(!HttpMethod.GET.matches(request.getMethod()) && lockFile.exists()){ + response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, + "Application is in maintenance mode. Only GET requests are supported."); + }else{ + filterChain.doFilter(servletRequest, servletResponse); + } + } + + /* + * (non-Javadoc) + * + * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) + */ + @Override + public void init(FilterConfig filterConfig) throws ServletException { + + } + +} diff --git a/src/main/java/org/wikitolearn/utils/DbConnection.java b/src/main/java/org/wikitolearn/utils/DbConnection.java index 2c7a3d2..df3bcf6 100644 --- a/src/main/java/org/wikitolearn/utils/DbConnection.java +++ b/src/main/java/org/wikitolearn/utils/DbConnection.java @@ -1,53 +1,53 @@ /** * */ package org.wikitolearn.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import com.orientechnologies.orient.core.intent.OIntentMassiveInsert; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory; import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; import org.wikitolearn.dao.UserDAO; /** * @author alessandro * */ @Component public class DbConnection { private static final Logger LOG = LoggerFactory.getLogger(UserDAO.class); private OrientGraphFactory factory; @Autowired - public DbConnection(@Value("${DB_URL}") String dbUrl, @Value("${DB_USER}") - String dbUser, @Value("${DB_PWD}") String dbPwd){ + public DbConnection(@Value("${db.url}") String dbUrl, @Value("${db.user}") + String dbUser, @Value("${db.password}") String dbPwd){ factory = new OrientGraphFactory(dbUrl, dbUser, dbPwd).setupPool(1, 20); } /** * This method will return the OreintDB graph instance after connection. * @return Transaction enabled OrientGraph object */ public OrientGraph getGraph(){ LOG.info("Getting an instance of OrientDB...."); return factory.getTx(); } /** * This method will return the OreintDB graph instance after connection, * for massive inserts to improve performance,no transaction method * @return Transaction disabled OrientGraph object */ public OrientGraphNoTx getDbGraphNT() { LOG.info("Getting a NoTX instance of OrientDB...."); factory.declareIntent(new OIntentMassiveInsert()); return factory.getNoTx(); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 0ef4f9b..ac660b0 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1,16 @@ -#Database connection properties -DB_URL= -DB_USER= -DB_PWD= \ No newline at end of file +# DATABASE +db.url= +db.user= +db.password= + +# MAINTENANCE +maintenance.uri= + +# SECURITY +security.basic.authorize-mode=role +security.basic.realm=Maintenance +security.basic.path=/admin/** +security.user.name= +security.user.role=ADMIN +security.user.password= +security.headers.xss=true