REST Call to Resource in Dropwizard/Jersey and UTF-8 works only on dev machine - utf-8

We're using dropwizard 0.7.1 which comes with jetty 9.0.7 and the jersey http client 1.18.1 (yes, it's old...). The os is linux, we're using Java 1.8.
I'm running an integration test locally inside eclipse, which makes a rest call using jersey to a dropwizard application running inside a vagrant box.
One of the tests should verify if I can send non-latin characters to the server.
I'm sending the string "Владимир Արման" inside a String field of a POJO using jersey:
req.type(MediaType.APPLICATION_JSON + "; charset=utf-8")//
.post(ClientResponse.class, user);
The resource I'm sending this too looks like this:
#Path("/users")
#Produces(MediaType.APPLICATION_JSON + "; charset=utf-8")
#Consumes(MediaType.APPLICATION_JSON + "; charset=utf-8")
public class UserServiceResource{
[...]
#POST
public Response createUser(UserCreate user) {...}
(you see we're both on the client and on the server side enforcing the utf-8 charset, which in my opinion, should actually not be necessary?)
This works perfectly locally, the name arrives correctly.
On jenkins, however, this does not work, I only receive "??????" in the service instead of the correct characters. In the log of the client that posts the pojo I still see the correct characters.
The setup is quite similar: jenkins builds a vagrant box, and then runs the tests using maven against this box (the integration test code runs outside the vagrant box, the service runs inside vagrant).
Jenkins uses maven, but when running the integration test locally using maven, it still works fine.
The vagrant box I'm using locally is also build and provisioned by jenkins, in a different job.
We are now trying to investigate if the environment settings might be slightly different, already tried setting
LANG=en_US.UTF-8
LC_ALL=en_US.UTF-8
MAVEN_OPTS="-Dfile.encoding=UTF-8"
but that didn't help.
What mainly puzzles me is that I would expect to work this under any environment settings, as we explicitly enforce UTF-8.
Are there know issues how the environment overrides the encodings set in the client and the server?

This problem just ate my soul. The root issue ended up being with the JDBC configuration. Originally my config looked like this:
database:
driverClass: com.mysql.jdbc.Driver
user: ...
password: ...
url: jdbc:mysql://...
properties:
charSet: UTF-8
hibernate.dialect: org.hibernate.dialect.MySQL5InnoDBDialect
maxWaitForConnection: 1s
validationQuery: "/* DropWizard Health Check */ SELECT 1"
minSize: 5
maxSize: 25
checkConnectionWhileIdle: false
checkConnectionOnBorrow: true
The solution was to update the properties block to define a characterEncoding and useUnicode property. Like this:
properties:
charSet: UTF-8
characterEncoding: UTF-8
useUnicode: true
hibernate.dialect: org.hibernate.dialect.MySQL5InnoDBDialect
Updating my YAML config, in addition to adding a charset parameter to the Content-Type header, fixed the issue for me.

Related

Client authentication failure on Neo4J

I basically followed the steps described here: https://docs.spring.io/spring-data/neo4j/docs/current/reference/html/#configure-spring-boot-project
My application.properties contains the following:
spring.neo4j.uri=neo4j://localhost:7687
spring.neo4j.authentication.username=neo4j
spring.neo4j.authentication.password=verySecret357
I have a Neo4jConfiguration Bean which only specifies the TransactionManager, rest is (supposedly) taken care of by spring-boot-starter-data-neo4j:
#Configuration
public class Neo4jConfiguration {
#Bean
public ReactiveNeo4jTransactionManager reactiveTransactionManager(Driver driver,
ReactiveDatabaseSelectionProvider databaseNameProvider) {
return new ReactiveNeo4jTransactionManager(driver, databaseNameProvider);
}
}
Neo4j (5.3.0) runs in a Docker container I started with
docker run -d --name neo4j -p 7474:7474 -p 7687:7687 -e 'NEO4J_AUTH=neo4j/verySecret357' neo4j:4.4.11-community
I can access it through HTTP on my localhost:7474 and can authenticate using the credentials above.
Now, when I run my springboot app and try to create Nodes in Neo4j, I keep getting the same exception:
org.neo4j.driver.exceptions.AuthenticationException: The client is unauthorized due to authentication failure.
Running in debug, it however seems the client authentication scheme is correctly set:
Any thoughts on what I might be doing wrong ?
Edit: one thing though, I would assume that the "authToken" would contain a base64-encoded String (username:password) as the scheme is basic auth. It looks like it's not the case (using neo4j-java-driver:5.2.0).
Edit: seems to be related to the Docker image. A standalone neo4j instance works fine.

Phoenix-framework : Mysql connection error on production

I am new to Phoenix and actually working on my first project in it.
When deploying the project on production server, I am getting a Database Connection error for mySql. For some reason, it is not considering the username/password values provided in config/prod.exs
import Config
config :g_plus, GPlusWeb.Repo,
username: "root",
password: "Somepassword",
database: "db_name",
hostname: "localhost",
load_from_system_env: true,
pool_size: 20
I also tried with environment variable (DATABSE_URL), but it is still not working.
ecto://root:Somepassword#localhost:3306/db_name
Am I missing any setting/config somewhere?
I couldn't find anything in Google search as well.
Most deployment guides are for apps without DB.
I found the issue. I was using GPlusWeb instead of just GPlus in the following line.
config :g_plus, GPlusWeb.Repo,
I changed it to config :g_plus, GPlus.Repo, and it worked.

"java.lang.IllegalStateException: Cannot load driver class" in Spring Boot application

I am using to configure spring boot with an external YAML configuration and CMD.
-> application.yml file
spring:
profiles: integration-test
datasource:
driverClassName: ${SPRING_DATASOURCE_DRIVER_CLASS_NAME}
url: ${SPRING_DATASOURCE_URL}
username: ${SPRING_DATASOURCE_USERNAME}
password: ${SPRING_DATASOURCE_PASSWORD}
-> cmd
mvn clean install
-> Result
Caused by: java.lang.IllegalStateException: Cannot load driver class: ${SPRING_DATASOURCE_DRIVER_CLASS_NAME}
Can anyone explain this to me?
When you use the syntax ${}, you are actually telling Spring Boot to use the value of the property whose name is between brackets. In your case, Spring Boot tries to resolve the property SPRING_DATASOURCE_DRIVER_CLASS_NAME. When it fails, it uses the string as is, which leads to the error you mentioned, since no driver exists under the name ${SPRING_DATASOURCE_DRIVER_CLASS_NAME}.
To solve the issue, you can either :
replace the ${} by the real values, e.g. driverClassName: org.postgresql.Driver and do the same for the other properties (url, username and password)
provide properties SPRING_DATASOURCE_DRIVER_CLASS_NAME,SPRING_DATASOURCE_URL and the two others. These can passed in the command line with -D options (e.g. -DSPRING_DATASOURCE_DRIVER_CLASS_NAME=org.postgresql.Driver) or through environment variables. You can look at spring Boot documentation for more details.
Pass those variables in your launch configuration of your program or at commandline when you run your app with java YourMainClass, e.g.
java -DSPRING_DATASOURCE_DRIVER_CLASS_NAME=<full_qualified_name_of_your_jdbc_driver_class> -DSPRING_DATASOURCE_URL=<jdbc_url> YourMainClass
also pass the other two variables the same way, username & password!
your can even set those enviroment variables on OS level, so you don't have to set them each time you start your application...
if your using Spring Boot also have a look at this one: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Spring cloud's config server plain text api with SVN and a default label

I have spring boot 2 app that acts as a config server with the following properties. Notice in particularly the "default-label" properties which is the empty string because we check out directly the folder that contains the files, and not some parent branch/folder.
spring:
application:
name: config-server
profiles:
include: subversion
cloud:
config:
server:
svn:
uri: https://...somesvnrepo.../project/trunk/config
username: fake
password: notreal
default-label:
basedir: C:\Users\John\Documents\Application\configserver_tmp
The contents of /trunk/config is straigthforward. There ae no subdirectories and just these files:
application.yml
application-dev.yml
myservice.yml
myservice-dev.yml
logback.xml
Serving the yml files works fine, but getting the logback.xml file using the "plain text api" not work at all.
Doing localhost:8888/appname/default/master/logback.xml gives the error "no label master found" which is true, I don't have that label. Any other combination of paths by omitting profiles or labels results in a 404 all the way up to just calling localhost:8888/logback.xml. Adding the ?useDefaultLabel request parameter makes no difference. Actually I don't understand the purpose of the appname, profile and label part of the url when the context is to get a plain text file that is not bound to any specific application, profile or label.
I found similar questions on the internet but they mention updating their spring boot version and then it worked for them. I'm already at the latest spring boot version (2.1.3-release).
Is this because I use SVN? Or because of of the default-label being empty?

Embedded Glassfish v3: deploying sun-resources.xml programmatically fails

I would like to be able to package my jpa-ejb-web project as a standalone application, by using Glassfish embedded API.
To use the JPA layer, I need to deploy the sun-resource.xml configuration, which should be possible with the asadmin command add-resources path\to\sun-resources.xml. I've this code to do it:
String command = "add-resources";
ParameterMap params = new ParameterMap();
params.add("", "...\sun-resources.xml" );
CommandRunner runner = server.getHabitat().getComponent(CommandRunner.class);
ActionReport report = server.getHabitat().getComponent(ActionReport.class);
runner.getCommandInvocation(command, report).parameters(params).execute();
but Glassfish refuses it with:
15-Jul-2010 16:34:12 org.glassfish.admin.cli.resources.AddResources execute
SEVERE: Something went wrong in add-resources
java.lang.Exception: ...\gfembed6930201441546233570tmp\lib\dtds\sun-resources_1_4.dtd (The system cannot find the path specified)
at org.glassfish.admin.cli.resources.ResourcesXMLParser.initProperties(ResourcesXMLParser.java:163)
at org.glassfish.admin.cli.resources.ResourcesXMLParser.<init>(ResourcesXMLParser.java:109)
at org.glassfish.admin.cli.resources.ResourcesManager.createResources(ResourcesManager.java:67)
at org.glassfish.admin.cli.resources.AddResources.execute(AddResources.java:106)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:305)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:320)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1176)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$900(CommandRunnerImpl.java:83)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1235)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1224)
at javaapplication4.Main.main(Main.java:55)
and indeed, there is no lib directory on the indicated path ...
is there something wrong in my code? (I use glassfish-embedded-all-3.0.1.jar)
Thanks
I solved it by specifying an Embedded File System for the embedded Glassfish, and prepopulated the /path/to/my/glassfish/lib/dtds folder with the files which were missing.
EmbeddedFileSystem.Builder efsb = new EmbeddedFileSystem.Builder();
efsb.autoDelete(false);
efsb.installRoot(new File("/path/to/my/glassfish"), true);
EmbeddedFileSystem efs = efsb.build();
Server.Builder builder = new Server.Builder("test");
builder.embeddedFileSystem(efs);
builder.logger(true);
Server server = builder.build();
server.addContainer(ContainerBuilder.Type.all);
server.start();
and asking Glassfish not to delete the folder at the end of the execution.
I'm not sure this is possible, Running asadmin Commands Using the Sun GlassFish Embedded Server API doesn't mention such a use case (passing a sun-resources.xml).
But I would use a preconfigured domain.xml instead of trying to deploy a sun-resource.xml file, the result should be similar. From the Sun GlassFish Enterprise Server v3 Embedded Server Guide:
Using an Existing domain.xml File
Using an existing domain.xml file
avoids the need to configure embedded
Enterprise Server programmatically in
your application. Your application
obtains domain configuration data from
an existing domain.xml file. You can
create this file by using the
administrative interfaces of an
installation of nonembedded Enterprise
Server. To specify an existing
domain.xml file, invoke the
installRoot, instanceRoot, or
configurationFile method of the
EmbeddedFileSystem.Builder class or
a combination of these methods.
The documentation provides code samples showing how to do this (should be pretty straightforward).

Resources