Is it possible to extract value from Header in Spring Cloud Gateway predicate via YAML config? - spring-webflux

I want to capture the value from my CountryCode header and use that to set my path, see this config for a simplified example of what I am trying to achieve:
spring:
cloud:
gateway:
routes:
- id: custom_route
uri: http://example.org
predicates:
- Path=/user
- Header=CountryCode, {countryCode:[A-Z]{2}}
filters:
- SetPath=/{countryCode}/user
- RemoveRequestHeader=CountryCode
Is it possible to do this with some kind of capture group, regex or pattern matching on the value of the CountryCode header? The example above is my understanding of how the pattern matching in Spring Cloud Gateway works, but the only example I could find of a header predicate was:
predicates:
- Header=X-Request-Id, \d+
and I can neither get my example to work, nor does the example appear to be able to capture the header value. I can imagine there is a way to do this in Java, but is it possible to do it in the application.yml instead?

Related

Use (scalar) variables in jsonschema to dynamically set attribute, e.g. title

I am using Google Cloud deployment manager to manage infrastructure as code (IAC) and they support providing schema files for describing IAC templates. Their support of jsonschema is a bit weird - the documentation is very brief but it suggests that they support the latest version of the schema plus they require title and description to be within an info object.
This is irritating because I use an HTML renderer for my schemas, which implements jsonschema and therefore, it requires title and description to be set as top-level properties.
To satisfy both, I need to duplicate, e.g.:
title: foo
description: bar
info:
title: foo
description: bar
I was hoping to just define title and description values once and then use some $ref: "#/$defs/title" magic but I don't think you can use this to dynamically set values like the title because this functionality is intended for schema parsers to fetch block content from elsewhere.
Is there any way I can avoid duplicating the values - beyond dynamically rendering my schema files which I do not want to do.
As far as I can tell, there is no way to use reference within json schema.
As a crude workaround you can use a script to add/replace placeholders:
#!/bin/bash
sed -i 's/\$title/title: foo/g' file.json
sed -i 's/\$desc/description: bar/g' file.json

Using variables in Serverless resource name

In my serverless.yml I have:
public-function:
handler: function.Handler
I want to parametrise the public-function line but I'm not sure how to do this. I know I can add a name: attribute to the function and use the ${} syntax documented at https://www.serverless.com/framework/docs/providers/aws/guide/variables/ but this only half solves my problem as public-function is used by Serverless as a basis for the names of other resources which I also want to have parametrised names.
Is there some way of using variables in the resource name?
The declared function key (i.e. public-function) is only a reference in the stack. There is no reason to change it at build time as the name is arbitrary.
If you want to customise the details of the deployed function, change its configuration.
For example, change the name of the function to something else using environment variables:
functions:
main:
handler: function.Handler
name: ${self:provider.stage}-${env:FUNCTION_NAME}
If you are exporting the resource to another stack, you can define the export name like so:
resources:
Outputs:
MainFunction:
Value:
Fn::GetAtt: [MainLambdaFunction, Arn]
Export:
Name: ${self:provider.stage}-${env:CUSTOM_EXPORT_NAME}
The stage scoping to both of these examples is recommended but not required; you can name your functions, resources, and outputs anything you want.

How to make resource in Serverless Framework reusable?

I am trying to set up my serverless.yml file with only an API Gateway, and there are several resources that I need to reuse throughout the file, and I'd like to not copy and paste the same lines of code per resource like 100 times.
For example, I want to reuse TestId resource below and dynamically pass in the Ref under ParentId so I can reuse the resource for multiple parent resources. How can I do that?
TestResource:
Type: AWS::ApiGateway::Resource
Properties:
ParentId:
Fn::GetAtt:
- TestApi
- RootResourceId
PathPart: test
RestApiId:
Ref: TestApi
TestId:
Type: AWS::ApiGateway::Resource
Properties:
ParentId:
Ref: TestResource
PathPart: '{id}'
RestApiId:
Ref: TestApi
Ideally I would want the resuable TestId resource in a separate file and then reference it, and also pass in the dynamic value that I want to insert in. Something like:
TestId: ${file(testid.yml):TestId} somehow override the TestId.Properties.ParentId.Ref: <somevalue>
Does anyone know if this is possible?
In case you are using NodeJs (if not maybe something like this exists in your programming language), I think you should take a look at this plugin : https://www.npmjs.com/package/yamlinc
It allow to compose YAML files using $include tag.
Hope this helps you.

JSONAPIAdapter - Use camel case instead of dasherized case

Is there a quick and easy way to use camel case when serializing a JsonApi model? By default is is using dasherized case for both url and field names.
Look at EmberJS guides for an example how to make user-profile-related adapter hit user_profile instead:
export default DS.JSONAPIAdapter.extend({
pathForType: function(type) {
return Ember.String.underscore(type);
}
});
Requests for person would now target /person/1. Requests for user-profile would now target /user_profile/1
If you need to serialize attributes, not just the model names, you can find related section to the topic at the very same place, direct link here.
Since the example above uses Ember.String.underscore(), I am attaching a link to very useful String helpers Ember provides by default, Ember.String API:
camelize
capitalize
classify
dasherize
decamelize
htmlSafe
loc
underscore
w

Cannot understand Apache Stanbol enhancer response (Semantic Web)

I am new to semantic web and trying to understand. I am using Apache Stanbol
I am seding text to enhance using Restful service and getting a Structure like
ID: enhancement-e7a7f095-d127-0b6f-16db-b89d181c8314
confidence: 0.9877373098687467
created: 2012-03-19T17:01:03.879Z
creator: org.apache.stanbol.enhancer.engines.opennlp.impl.NEREngineCore
end: 1458
extracted-from: content-item-sha1-18a53d775839d36bf1cc220b3a3fa813a6a64593
relation: enhancement-92681e65-f8c8-fb90-aed2-de05f7898ab9
selected-text: Cologne
start: 1451
type: Place,TextAnnotation,Enhancement
What I wanted to ask is what does start, end means? and relation contains a comma seprated list of IDs. How may I access the related IDs using SPARQL
start: The start character position within the plain text version of the parsed content. Note that the plain text version can be
retrieved by using the multi-part content item support of the Stanbol
Enhancer RESTful API.
end: The end character position. This MUST only be present of "fise:start" is also defined.
relation: Specifies that the current fise:Enhancement has a relation to an other fise:Enhancement. Values need to be resources of the "rdf:type" "fise:Enhancement".
from Apache Stanbol online documentation.