Disable java checkstyle text block line length - checkstyle

I am building against java 17 using
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.2.1</version>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>10.7.0</version>
</dependency>
</dependencies>
...
With the google-check.xml configuration.
My class has a couple of static final Strings containing SVG's using the java triple """ text blocks with lines much longer than the normal allowed line length.
I've read that there is support in checkstyle from 10.6.0 for TextBlocks but have no idead how to enable it, or how, if it can be done, to switch off the line length check on the static final SVG strings in my code.
Any help would be appreciated.
I've tried the broad brush:
//CHECKSTYLE:OFF
...
//CHECKSTYLE:ON
Around the """ text blocks and using #SuppressWarnings("checkstyle:LineLegth") and #SuppressWarnings("LineLegth") and all mixes of case but nothing seems to work

Google check's suppression with CHECKSTYLE:ON/CHECKSTYLE:OFF requires the name of the check in the comment to do the suppression.
Google check does support suppressing with #SuppressWarnings and requires the text in the format of "checkstyle:name_of_the_check". Without an example of what you are doing, I can't say more why it isn't working.
I recommend using the SuppressionFilter and use the XML file as your storage of suppressions.
More information and some small examples can be found at https://checkstyle.org/google_style.html#Google_Suppressions

Related

I have installed OpenAPI 3 using springdoc, but the URL is strange. Can I change it to the expected value?

I have installed swagger-ui using the following artifacts in my Java project's pom file:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-security</artifactId>
<version>1.5.2</version>
</dependency>
I can view the swagger ui for my RESTful endpoints when I go to this URL
http://localhost:8081/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config
but not this link
http://localhost:8081/swagger-ui/index.html
Why is this? How can I change it back to the expected URL?
Firstly, the below dependency has nothing to do with the issue you are mentioning.
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-security</artifactId>
<version>1.5.2</version>
</dependency>
Now to answer your question.
The URL http://localhost:8081/swagger-ui/index.html comes from the Swagger-Core library upon which Springdoc is built and thus it will serve the default Petstore example. Though this page can be disabled using the below property in application.properties file.
springdoc.swagger-ui.disable-swagger-default-url=true
But that holds only for version v1.4.1 and above.
Why /v3/api-docs/swagger-config ?
Now in the URl http://localhost:8081/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config the query parameter configUrl specifies from where to fetch the Springdoc's Swagger-specific configuration file.
Now if you actually hit the URL http://localhost:8081/v3/api-docs/swagger-config, you'll get a config file that's similar to the one below
{
"configUrl": "/v3/api-docs/swagger-config",
"docExpansion": "none",
"urls": [
{
"url": "/v3/api-docs/default",
"name": "default"
}
],
"validatorUrl": ""
}
The urls object encompasses individual objects for each API-group. Each API group is shown in the Swagger-UI as shown below
Now, for each API group, these properties are used by Springdoc to render the Swagger-UI by fetching the OpenAPI specification file from the given url and the name is rendered as shown in the above image.
As for the docExpansion property, it's set to none as that setting was explicitly specified in my application.properties
Playing with Properties
As of Springdoc 1.6.0, the below property is blocked by default for security reasons. Please refer to the comment here for more info. Use springdoc.swagger-ui.queryConfigEnabled=true to enable it at own risk since 1.6.0.
Use the below property if you want to override the URL from where Springdoc's Swagger configuration file is fetched. Essentially the new URL should return a config file that's similar to the one returned from /v3/api-docs/swagger-config. Defaults to /v3/api-docs/swagger-config
springdoc.swagger-ui.configUrl=/mySpringdocConfig
Use the below property to override the URL where your Springdoc's Swagger-UI loads. Defaults to /swagger-ui.html, and that's why Swagger-UI is available at http://localhost:8081/swagger-ui.html
springdoc.swagger-ui.path=/mySwagger-UIPath
Conclusion
I don't think it's possible to get rid of the ?configUrl=/v3/api-docs/swagger-config from the URL, as it'll always be available and point to the location from where Sprinfdoc's configuration file will be fetched, which if not available will lead to an error in fetching the configuration file and thus make the Swagger-UI useless. Also, it's something you'll not want to get rid of as it's being used by the framework itself.
Refer here for a list of all the supported properties to customize the behavior of Springdoc.
You need to access the swagger ui at http://localhost:8081/swagger-ui.html instead of http://localhost:8081/swagger-ui/index.html which loads the pet store default swagger. You can check the work around at
https://github.com/springdoc/springdoc-openapi/issues/43
The url http://localhost:8081/swagger-ui.html will always get redirected to http://localhost:8081/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config.
You can customize with the following in application.properties
springdoc.api-docs.path=/api-docs
springdoc.swagger-ui.path=/swagger-ui-custom.html
Now the url http://localhost:8081/swagger-ui-custom.html will get redirected to http://localhost:8081/swagger-ui/index.html?configUrl=/api-docs/swagger-config.
Below configuration helped customising the paths:
springdoc.swagger-ui.path=docs/ui
springdoc.swagger-ui.url=/api/openapi.json
springdoc.swagger-ui.disable-swagger-default-url=true
/api is context path for application
In the browser, I can navigate to http://localhost:8080/docs/ui which further redirects to http://localhost:8080/docs/swagger-ui/index.html as the default behaviour (which seems to be unavoidable). However, configUrl is not present at the end of redirected url.
Dependency used:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.6</version>
</dependency>

Can we replace swagger-ui with redoc in Springdoc

I'm trying to replace a manually-maintained swagger file with Springdoc. Currently, we use ReDoc to render the file, because of their support for discriminator.
Is there a straightforward way to replace/supersede the embedded swagger-ui with ReDoc?
In springdoc you can skip using the swagger UI package and use:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-webmvc-core</artifactId>
</dependency>
instead. Then you can point to the location of swagger schema generated with springdoc (default is /v3/api-docs) in your <redoc /> component. The rest is depending on how you host Redoc. In our case we just added a controller to our service/app that serves a basic HTML described on Redoc documentation page (https://github.com/Redocly/redoc#tldr).
You can disable the default swagger-ui with
springdoc.swagger-ui.enabled=false
see the "Disabling the swagger-ui" section at https://springdoc.org/
After that, all you need to do is put ReDoc on your server and have it point to the OpenAPI description usually at /context-path/v3/api-docs
See the "Deployment" section at https://github.com/Redocly/redoc

Generated protoc file creates a target source with error

I have tried to make a project that connects to a DB and takes information and outputes them in a desktop application through grpc.
But, when I compile it I get "Error:(20,18) java: cannot find symbol" in a target file( which is auto-generated by protoc.
I can't understand the issue with this. I have tried to change the compiler of mvn, to change the version on protoc, to setup a different JDK, everything just gets worse that it already is.
My project can be viewed here : https://github.com/Diana-Ioana/grpc
My db and error with the generated target files that crashes is : https://imgur.com/a/T6taLXF
I have no idea what to do now. Any help would be greate, thank you!
It looks like the "cannot find symbol" is referring to javax.annotation.Generated. In that case you can add a dependency on annotations-api:
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>annotations-api</artifactId>
<version>6.0.53</version>
<!-- Generated has #Retention(SOURCE), so not needed at runtime -->
<scope>provided</scope>
</dependency>
Originally this answer suggested javax.annotation-api, but that library is licensed CDDL so gRPC changed its recommendation.
<!-- The old suggestion. Uses CDDL-licensed library -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency>

Enabling LZMA(2) (i.e. `.xz`) compression in log4j2

Current state of world
Currently our RollingFileAppender in log4j2.xml uses Gzip compression:
<RollingFile name="RollingFile"
fileName="logs/engine.log"
filePattern="logs/engine.log.%i.gz">
Goal
I would like to switch to LZMA(2) (i.e. .xz) compression, to enjoy an improved compression ratio.
Attempt
I have tried changing engine.log.%i.gz to engine.log.%i.xz — as per the documentation:
If the file pattern ends with .gz, .zip, .bz2, .deflate, .pack200, or .xz the resulting archive will be compressed using the compression scheme that matches the suffix. The formats bzip2, Deflate, Pack200 and XZ require Apache Commons Compress. In addition, XZ requires XZ for Java.
Additionally I have ensured that I have a runtime dependency on XZ for Java — via pom.xml:
<dependency>
<!-- Support Log4j2 Log compression schemes: ".gz", ".zip", ".bz2", ".deflate", ".pack200", [".xz" (part 1 of 2)] -->
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<!-- Support Log4j2 Log compression scheme [".xz" (part 2 of 2)] -->
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>1.5</version>
</dependency>
Result
When the RollingFileAppender is triggered: the archive created is indeed named engines.log.1.xz — as required.
However, its contents are incorrect:
Expectation
engines.log.1.xz should contain LZMA(2) compressed text
Actual
engines.log.1.xz instead contains plain, uncompressed text.
Sanity checks
I confirm that the org.tukaani:xz and org.apache.commons:commons-compress successfully made it into the classpath of my jar:
🍔 jar tf mycooljar.jar | grep tukaani
org/tukaani/
org/tukaani/xz/
…
🍔 jar tf mycooljar.jar | grep org/apache/commons/compress
org/apache/commons/compress/
org/apache/commons/compress/changes/
…
This Java program is not deployed to a J2EE webserver. I believe its class loading is straightforward.
Summary
I have correctly followed the instructions necessary to create .gz archives.
I believe the only additional step required to create .xz archives is: I must provide at runtime the XZ for Java artefact. I have done this.
Am I missing something here? I am tempted to believe one of the following:
The functionality is broken
The docs are incomplete/incorrect
log4j2 fails to discover the class at runtime
Remko Popma confirmed that this is a bug:
log4j-core accepted only *.xy as a matching pattern — whereas the docs suggested that *.xz was the required input.
"xy" was being passed into new CommonsCompressAction(…) instead of the required "xz".
These were both presumed to be typos: the proposed resolution was to change both to xz.
Gary Gregory wrote a fix. The fix is presently in master of org.apache.logging.log4j:log4j-core:2.6-SNAPSHOT, so should release with 2.6.

Thymeleaf - Strict HTML parsing issue

HTML5 allows some tags to be written more liberally i.e without corresponding END tags. e.g. input need not be closed </input>.However if choose template mode HTML5 in Thymeleaf the Thymeleaf engine complains about this and does not parse the HTML template. I want to override this default Strict tag checking behavior. i.e Thymeleaf should parse an HTML template with meta and input (AND ALIKE) tags WITHOUT THEIR RESP. CLOSING TAGS. Pl. guide.
It also complains when you have something like this
<a href="/home/pic/image.png" download="/path/to/file" data-gallery></a>
It throws an exception when it encounters the data-gallery throws "should be followed by '=' " which is kind of annoying as it takes the flexibility out of HTML5.
All you have to do is run Thymeleaf in "LEGACYHTML5" mode and it works like a charm. Thanks to this and this post, I found the solution and am documenting in SO so others do not have to go through the same trouble in finding this answer.
To set the legacy mode you can define the bean in your Spring XML file:
<!-- View TemplateResolver -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="templateMode" value="LEGACYHTML5"/>
<property name="cacheable" value="false"/>
</bean>
or add the properties to the application.properties file:
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false
And in both cases you have to add the nekohtml jar to your project or, if you are running maven, you can add its dependency to your pom.xml
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.21</version>
</dependency>
Gradle
'net.sourceforge.nekohtml:nekohtml:1.9.21'
Here is how you can do it in a neat way
Step 1: Add thymeleaf mode to your application.properties file.
resources/application.properties
spring.thymeleaf.mode=LEGACYHTML5
Step 2: Add nekohtml dependency to your pom.xml file.
pom.xml
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>
Using LEGACYHTML5 worked for me as well.
Also it is necessary to add
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>
to pom.xml as stated above. But there is one more step which might occur. After doing those two steps I was getting:
java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal
To avoid this it is necessary to add
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
to pom.xml