Skip to content

Commit

Permalink
Merge pull request #35 from Zenika/zenika-dev
Browse files Browse the repository at this point in the history
027b WIP
  • Loading branch information
bwerquin authored Sep 5, 2017
2 parents 89ce602 + f028cbb commit 8cb2ffa
Show file tree
Hide file tree
Showing 40 changed files with 727 additions and 111 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* text=auto
*.sh text eol=lf
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ script:
- mvn test
- bash scripts/coveralls.sh
- mvn integration-test
after_script:
- bash scripts/gitbook.sh
after_success:
- bash scripts/tag.sh
before_deploy:
- bash scripts/build.sh rmspogfo
- bash scripts/tag.sh
deploy:
provider: releases
api_key:
Expand All @@ -32,7 +31,10 @@ deploy:
on:
repo: InseeFr/Pogues-Back-Office
branch: zenika-dev
condition: -f "$TRAVIS_BUILD_DIR/.tag" && $(cat "$TRAVIS_BUILD_DIR/.tag") =~ v[0-9]\.[0-9]\.[0-9].*
tags: false
after_script:
- bash scripts/gitbook.sh
branches:
except:
- "/^v[0-9]+\\.[0-9]+\\.[0-9]+.*/"
1 change: 1 addition & 0 deletions docker/colectica/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM vimagick/json-server
140 changes: 140 additions & 0 deletions docker/colectica/db.json

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ services:
networks:
intra:
ipv4_address: 172.20.0.3
colectica:
build: ./colectica
command: -w db.json --id Identifier
networks:
intra:
ipv4_address: 172.20.0.4
volumes:
- ./colectica/db.json:/app/db.json
restart: always
tomcat:
build: ./tomcat
volumes:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>fr.insee</groupId>
<artifactId>Pogues-BO</artifactId>
<packaging>war</packaging>
<version>0.0.12-SNAPSHOT</version>
<version>0.0.14-SNAPSHOT</version>
<name>Pogues-BO</name>

<properties>
Expand Down
1 change: 1 addition & 0 deletions scripts/tag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function tag() {
git remote add upstream "$UPSTREAM"
git tag --annotate "${TAG}" -m "${MESSAGE}"
git push upstream --tags
echo "$TAG">"$TRAVIS_BUILD_DIR/.tag"
}

function main(){
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/fr/insee/pogues/config/ApplicationContext.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
package fr.insee.pogues.config;

import org.apache.http.client.HttpClient;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

@Configuration
@PropertySource("classpath:env/${fr.insee.pogues.env:dv}/pogues-bo.properties")
public class ApplicationContext {

@Bean
public HttpClient httpClient(){
return HttpClients.createDefault();
public HttpClient httpClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
SSLContext sslContext = SSLContexts.custom()
.loadTrustMaterial(null, new TrustSelfSignedStrategy())
.build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslContext,
NoopHostnameVerifier.INSTANCE);
return HttpClients.custom()
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.setSSLSocketFactory(sslsf)
.build();
}
}
22 changes: 16 additions & 6 deletions src/main/java/fr/insee/pogues/config/SecurityContext.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.insee.pogues.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
Expand All @@ -20,6 +21,19 @@ public class SecurityContext extends WebSecurityConfigurerAdapter {
@Autowired
private Environment env;

@Value("${fr.insee.pogues.permission.ldap.hostname}")
String ldapHost;

@Value("${fr.insee.pogues.permission.ldap.root}")
String rootDn;

@Value("${fr.insee.pogues.permission.ldap.user.base}")
String userDn;

@Value("${fr.insee.pogues.permission.ldap.unite.base}")
String groupDn;


@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
Expand All @@ -39,21 +53,17 @@ protected void configure(HttpSecurity http) throws Exception {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
String ldapUserDn = env.getProperty("fr.insee.pogues.permission.ldap.user.base");
String ldapGroupDn = env.getProperty("fr.insee.pogues.permission.ldap.unite.base");
auth.ldapAuthentication()
.userSearchBase(ldapUserDn)
.userSearchBase(userDn)
.userSearchFilter("(uid={0})")
.groupSearchBase(ldapGroupDn)
.groupSearchBase(groupDn)
.groupSearchFilter("member={0}")
.contextSource(contextSource())
.passwordCompare().passwordAttribute("uid");
}

@Bean
public DefaultSpringSecurityContextSource contextSource() {
String ldapHost = env.getProperty("fr.insee.pogues.permission.ldap.hostname");
String rootDn = env.getProperty("fr.insee.pogues.permission.ldap.root");
return new DefaultSpringSecurityContextSource(Arrays.asList(ldapHost), rootDn);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fr.insee.pogues.metadata.client;

import org.json.simple.JSONObject;

public interface MetadataClient {

JSONObject getItem(String id) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package fr.insee.pogues.metadata.client;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class MetadataClientImpl implements MetadataClient {

@Autowired
HttpClient httpClient;

@Value("${fr.insee.pogues.api.remote.metadata.url}")
String serviceUrl;

@Override
public JSONObject getItem(String id) throws Exception{
try {
HttpGet get = new HttpGet(String.format("%s/items/%s", serviceUrl, id));
// HttpGet get = new HttpGet(String.format("https://dvrmesgopswas01.ad.insee.intra/api/v1/item/fr.insee/%s/1?api_key=ADMINKEY", id));
get.addHeader("accept", "application/json");
HttpResponse response = httpClient.execute(get);
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}
String body = EntityUtils.toString(response.getEntity());
return (JSONObject)
new JSONParser().parse(body);
} catch (Exception e) {
throw e;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fr.insee.pogues.metadata.model;

public class MetadataResponse {

String version;
String Identifier;
String Item;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fr.insee.pogues.metadata.repository;

import org.json.simple.JSONObject;

public interface MetadataRepository {

JSONObject findById(String id) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package fr.insee.pogues.metadata.repository;

import fr.insee.pogues.metadata.client.MetadataClient;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MetadataRepositoryImpl implements MetadataRepository {

@Autowired
MetadataClient metadataClient;

@Override
public JSONObject findById(String id) throws Exception{
try {
return metadataClient.getItem(id);
} catch (Exception e) {
throw e;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package fr.insee.pogues.metadata.service;

import fr.insee.pogues.search.model.Family;
import org.json.simple.JSONObject;

public interface MetadataService {

JSONObject getItem(String id) throws Exception;
Family getFamily(String id) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package fr.insee.pogues.metadata.service;

import fr.insee.pogues.metadata.repository.MetadataRepository;
import fr.insee.pogues.metadata.utils.XpathProcessor;
import fr.insee.pogues.search.model.Family;
import fr.insee.pogues.search.model.Operation;
import fr.insee.pogues.search.model.Questionnaire;
import fr.insee.pogues.search.model.Series;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.List;

@Service
public class MetadataServiceImpl implements MetadataService {

@Autowired
MetadataRepository metadataRepository;

@Autowired
XpathProcessor xpathProcessor;

@Override
public JSONObject getItem(String id) throws Exception{
try {
return metadataRepository.findById(id);
} catch(Exception e) {
e.printStackTrace();
throw e;
}
}

public Family getFamily(String id) throws Exception {
Family family = new Family();
String fragment = getItem(id).get("Item").toString();
String groupExp = "//*[local-name()='Group']";
String labelExp = "//*[local-name()='Citation']/*[local-name()='Title']/*[local-name()='String']/text()";
Node rootNode = xpathProcessor.queryList(fragment, groupExp).item(0);
family.setId(id);
family.setLabel(xpathProcessor.queryText(rootNode, labelExp));
family.setSeries(getSeries(rootNode, family));
return family;
}

public List<Series> getSeries(Node node, Family family) throws Exception {
List<Series> seriesList = new ArrayList<>();
String childExp = ".//*[local-name()='SubGroupReference']";
NodeList children = xpathProcessor.queryList(node, childExp);
for(int i = 0; i < children.getLength(); i++) {
String id = xpathProcessor.queryText(children.item(i),".//*[local-name()='ID']/text()");
String fragment = getItem(id).get("Item").toString();
System.out.println("fragment " + fragment);
Node child = xpathProcessor.toDocument(fragment);
Series series = new Series();
series.setId(id);
series.setParent(family.getId());
series.setLabel(xpathProcessor.queryText(child, ".//*[local-name()='SubGroup']/*[local-name()='Citation']/*[local-name()='Title']/*[local-name()='String']/text()"));
series.setOperations(getOperations(child, series));
seriesList.add(series);
}
return seriesList;
}

public List<Operation> getOperations(Node node, Series series) throws Exception {
List<Operation> operations = new ArrayList<>();
System.out.println(node.getLocalName());
String childExp = ".//*[local-name()='StudyUnitReference']";
NodeList children = xpathProcessor.queryList(node, childExp);
for(int i = 0; i < children.getLength(); i++) {
String id = xpathProcessor.queryText(children.item(i),".//*[local-name()='ID']/text()");
String fragment = getItem(id).get("Item").toString();
System.out.println("id: " + id);
System.out.println("frg : " + fragment);
Node child = xpathProcessor.toDocument(fragment);
Operation operation = new Operation();
operation.setId(id);
operation.setParent(series.getId());
operation.setLabel(xpathProcessor.queryText(child, ".//*[local-name()='StudyUnit']/*[local-name()='Citation']/*[local-name()='Title']/*[local-name()='String']/text()"));
operation.setQuestionnaires(getQuestionnaires(child, operation));
operations.add(operation);
}
return operations;
}

public List<Questionnaire> getQuestionnaires(Node node, Operation operation) throws Exception {
List<Questionnaire> questionnaires = new ArrayList<>();
String childExp = ".//*[local-name()='DataCollectionReference']";
NodeList children = xpathProcessor.queryList(node, childExp);
for(int i = 0; i < children.getLength(); i++) {
String id = xpathProcessor.queryText(children.item(i),".//*[local-name()='ID']/text()");
String fragment = getItem(id).get("Item").toString();
Node child = xpathProcessor.toDocument(fragment);
Questionnaire questionnaire = new Questionnaire();
questionnaire.setId(id);
questionnaire.setParent(operation.getId());
questionnaire.setLabel(xpathProcessor.queryText(child, ".//*[local-name()='DataCollection']/*[local-name()='Label']/*[local-name()='Content']/text()"));
questionnaires.add(questionnaire);
}
return questionnaires;
}
}
14 changes: 14 additions & 0 deletions src/main/java/fr/insee/pogues/metadata/utils/XpathProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fr.insee.pogues.metadata.utils;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public interface XpathProcessor {

NodeList queryList(String fragment, String xpathExpression) throws Exception;
NodeList queryList(Node node, String xpathExpression) throws Exception;
String queryText(String fragment, String xpathExpression) throws Exception;
String queryText(Node node, String xpathExpression) throws Exception;
Document toDocument(String xml) throws Exception;
}
Loading

0 comments on commit 8cb2ffa

Please sign in to comment.