Skip to content

Commit

Permalink
JSON-LD Context generator (Islandora#33)
Browse files Browse the repository at this point in the history
* JSON-LD Context generator

Base pull, service, interface and class. Needs testing. DCS should be
solved already

* Missing an @

* Wrong Cache backend service injected

* make sure the bundle has mapping

* Testing routes to make debugging easier

* phpstorm, leave my indentation alone!

* phpcs for test controller

* Changes, lots of them

* Exceptions thrown all around (and documented
* “Try/Catch” where relevant
* Logger channel for ISLANDORA, useful for all CLAW
* Exceptions are being cached
* Naive field types of json-ld term definitions for context. Kinda poor
mans rdf map for fields
 * Docs, docs.
 * Route Controller now responds with application/ld+json, means don’t
wait for HTML!
 * Concerns addressed

TODO: need new tests.

* Web tests!

Don’t run via UI (buggy) https://www.drupal.org/node/2745123
Do this
```Shell
sudo -u www-data php core/scripts/run-tests.sh --verbose --class
"Drupal\islandora\Tests\Web\JsonldContextGeneratorWebTest"
```

* Coding standards

* Coding standards and Cache

Now caching happens on the response and on the method. Best of both
worlds.
```Shell
curl -i http://localhost:8000/fedora_resource_context/rdf_source
HTTP/1.1 200 OK
Date: Tue, 21 Mar 2017 19:19:03 GMT
Server: Apache/2.4.18 (Ubuntu)
Cache-Control: must-revalidate, no-cache, private
X-Powered-By: Islandora CLAW API
X-Drupal-Dynamic-Cache: MISS
X-UA-Compatible: IE=edge
Content-language: en
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Expires: Sun, 19 Nov 1978 05:00:00 GMT
X-Generator: Drupal 8 (https://www.drupal.org)
X-Debug-Token: 7d33c2
X-Debug-Token-Link: /admin/reports/profiler/view/7d33c2
X-Drupal-Cache: HIT
Content-Length: 229
Content-Type: application/ld+json

{"@context":{"schema":"http://schema.org/","schema:dateModified":{"@type
":"xsd:dateTime"},"schema:dateCreated":{"@type":"xsd:dateTime"},"fedora"
:"http://fedora.info/definitions/v4/repository#","fedora:hasParent":{"@t
ype":"@id"}}}
````

and after cache clear (or changing user permissions or even an entity
type def associated to the requested rdfmapping)

```Shell
HTTP/1.1 200 OK
Date: Tue, 21 Mar 2017 19:20:49 GMT
Server: Apache/2.4.18 (Ubuntu)
Cache-Control: must-revalidate, no-cache, private
X-Powered-By: Islandora CLAW API
X-Drupal-Dynamic-Cache: MISS
X-UA-Compatible: IE=edge
Content-language: en
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Expires: Sun, 19 Nov 1978 05:00:00 GMT
X-Generator: Drupal 8 (https://www.drupal.org)
X-Debug-Token: acc399
X-Debug-Token-Link: /admin/reports/profiler/view/acc399
X-Drupal-Cache: MISS
Content-Length: 229
Content-Type: application/ld+json
````

* Short notion array..

should be named “bracket structure…”

* We should run tests on our own server...

* lets try with 127.0.0.1

* testing travis changes (Islandora#1)

* fixes not working Drupal/drush integration on Travis-CI

* Kernel tests

* 400 is 1 less than 401

* restore notifications

* Fixes type in TODO

* Jared rocks

* Debug statement not needed

Was not outputting anyway

* Namespace change

Addressing @dhlamb namespace change
  • Loading branch information
DiegoPino authored and dannylamb committed Mar 27, 2017
1 parent 779a9ee commit 6ced405
Show file tree
Hide file tree
Showing 11 changed files with 867 additions and 21 deletions.
10 changes: 5 additions & 5 deletions .scripts/travis_setup.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
echo "Setup database for Drupal"
mysql -u root -e 'create database drupal;'
mysql -u root -e "GRANT ALL PRIVILEGES ON drupal.* To 'drupal'@'localhost' IDENTIFIED BY 'drupal';"
mysql -u root -e "GRANT ALL PRIVILEGES ON drupal.* To 'drupal'@'127.0.0.1' IDENTIFIED BY 'drupal';"

if [ $TRAVIS_PHP_VERSION = "5.6" ]; then
phpenv config-add $SCRIPT_DIR/php56.ini
Expand Down Expand Up @@ -30,11 +30,11 @@ phpenv rehash

echo "Drush setup drupal site"
cd web
drush si --db-url=mysql://drupal:drupal@localhost/drupal --yes
drush runserver --php-cgi=$HOME/.phpenv/shims/php-cgi localhost:8081 &>/tmp/drush_webserver.log &

drush si --db-url=mysql://drupal:drupal@127.0.0.1/drupal --yes
drush runserver 127.0.0.1:8282 &
until curl -s 127.0.0.1:8282; do true; done > /dev/null
echo "Enable simpletest module"
drush en -y simpletest
drush --uri=127.0.0.1:8282 en -y simpletest

echo "Setup ActiveMQ"
cd /opt
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ install:
- git -C "$TRAVIS_BUILD_DIR" checkout -b travis-testing
- cd $DRUPAL_DIR; composer config repositories.local path "$TRAVIS_BUILD_DIR"
- composer require "islandora/islandora:dev-travis-testing as dev-8.x-1.x" --prefer-source
- cd web; drush en -y islandora
- cd web; drush --uri=127.0.0.1:8282 en -y islandora

script:
- $SCRIPT_DIR/line_endings.sh $TRAVIS_BUILD_DIR
- phpcs --standard=Drupal --ignore=*.md --extensions=php,module,inc,install,test,profile,theme,css,info $TRAVIS_BUILD_DIR
- phpcpd --names *.module,*.inc,*.test,*.php $TRAVIS_BUILD_DIR
- php core/scripts/run-tests.sh --verbose --php `which php` islandora
- php core/scripts/run-tests.sh --url http://127.0.0.1:8282 --verbose --php `which php` --module "islandora"

notifications:
irc:
Expand Down
27 changes: 13 additions & 14 deletions islandora.routing.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
# Islandora Routing definition
entity.fedora_resource_type.rdftest:
path: '/fedora_resource/{fedora_resource}/rdf'
defaults:
_controller: '\Drupal\node\Controller\NodePreviewController::view'
_title_callback: '\Drupal\node\Controller\NodePreviewController::title'
requirements:
_node_preview_access: '{node_preview}'
options:
parameters:
node_preview:
type: 'node_preview'

# Menu list of Islandora configuration forms
# Menu list of Islandora configuration forms
system.admin_config_islandora:
path: '/admin/config/islandora'
defaults:
Expand All @@ -28,3 +15,15 @@ system.islandora_settings:
_title: 'Islandora Settings'
requirements:
_permission: 'administer site configuration'

# Islandora JSON-LD Routing definition
entity.fedora_resource_type.jsonldcontext:
path: '/fedora_resource_context/{bundle}'
defaults:
_controller: '\Drupal\islandora\Controller\FedoraResourceJsonLdContextController::content'
requirements:
_permission: 'access content'
options:
parameters:
bundle:
type: entity:{fedora_resource}:{fedora_resource_type}
6 changes: 6 additions & 0 deletions islandora.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ services:
islandora.versioncounter:
class: Drupal\islandora\VersionCounter\VersionCounter
arguments: ['@database']
logger.channel.islandora:
parent: logger.channel_base
arguments: ['islandora']
islandora.jsonldcontextgenerator:
class: Drupal\islandora\JsonldContextGenerator\JsonldContextGenerator
arguments: ['@entity_field.manager','@entity_type.bundle.info','@entity_type.manager', '@cache.default', '@logger.channel.islandora']
88 changes: 88 additions & 0 deletions src/Controller/FedoraResourceJsonLdContextController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Drupal\islandora\Controller;

use Drupal\Core\Cache\CacheableJsonResponse;
use Drupal\Core\Controller\ControllerBase;
use Drupal\islandora\JsonldContextGenerator\JsonldContextGeneratorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\rdf\Entity\RdfMapping;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableMetadata;

/**
* Class FedoraResourceJsonLdContextController.
*
* @package Drupal\islandora\Controller
*/
class FedoraResourceJsonLdContextController extends ControllerBase {

/**
* Injected JsonldContextGenerator.
*
* @var \Drupal\islandora\JsonldContextGenerator\JsonldContextGeneratorInterface
*/
private $jsonldContextGenerator;

/**
* FedoraResourceJsonLdContextController constructor.
*
* @param \Drupal\islandora\JsonldContextGenerator\JsonldContextGeneratorInterface $jsonld_context_generator
* Injected JsonldContextGenerator.
*/
public function __construct(JsonldContextGeneratorInterface $jsonld_context_generator) {
$this->jsonldContextGenerator = $jsonld_context_generator;
}

/**
* Controller's create method for dependecy injection.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The App Container.
*
* @return static
* An instance of our islandora.jsonldcontextgenerator service.
*/
public static function create(ContainerInterface $container) {
return new static($container->get('islandora.jsonldcontextgenerator'));
}

/**
* Returns an JSON-LD Context for a fedora_resource bundle.
*
* @param string $bundle
* Route argument, a bundle.
* @param \Symfony\Component\HttpFoundation\Request $request
* The Symfony Http Request.
*
* @return \Symfony\Component\HttpFoundation\Response
* An Http response.
*/
public function content($bundle, Request $request) {

// TODO: expose cached/not cached through
// more varied HTTP response codes.
try {
$context = $this->jsonldContextGenerator->getContext('fedora_resource.' . $bundle);
$response = new CacheableJsonResponse(json_decode($context), 200);
$response->setEncodingOptions(JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
$response->headers->set('X-Powered-By', 'Islandora CLAW API');
$response->headers->set('Content-Type', 'application/ld+json');

// For now deal with Cache dependencies manually.
$meta = new CacheableMetadata();
$meta->setCacheContexts(['user.permissions', 'ip', 'url']);
$meta->setCacheTags(RdfMapping::load('fedora_resource.' . $bundle)->getCacheTags());
$meta->setCacheMaxAge(Cache::PERMANENT);
$response->addCacheableDependency($meta);
}
catch (\Exception $e) {
$response = new Response($e->getMessage(), 400);
}

return $response;
}

}
Loading

0 comments on commit 6ced405

Please sign in to comment.