MismatchedInputException: No content to map due to end-of-input

Viewed 216

I have got this code to parse json:

package com.mycompany.post1;
import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.fasterxml.jackson.databind.*;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;


import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
//import org.apache.commons.logging.*;
import org.json.*;
public class Post1 {

    public static String generateAuthHeader(String dataToSign, String secret)
   {      
    String hash = "";
      try { 

          Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
          SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
          sha256_HMAC.init(secret_key);
          hash = Base64.encodeBase64String(sha256_HMAC.doFinal(dataToSign.getBytes()));
      }
      catch (Exception e){
          System.out.println("Error");
      }
        return(hash);
   }
    

    // A simple request to create a Case using Http Post
    public static void main(String[] args) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            Date now = new Date();
            
            //format for date string Mon, 27 Mar 2017 15:19:36 GMT 
            DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
            df.setTimeZone(TimeZone.getTimeZone("GMT"));
            
            String date = df.format(now);
            String gatewayurl = "/v2/";
            String gatewayhost = "api-worldcheck.refinitiv.com";
            String apikey = "XXXXXXXXXXX-10fd-XXXX1-a5b5-XXXXXXXXXX";//ganarated from company
            String apisecret = "YYYYYYYYYYYYYVVVVVVVVVVVVVVVVVVVV==";//ganarated from company
            String groupid = "Ksgknsdkfgs-fsdg-dsfdgs-dfgsdc";

            //create a JSON string 
            String jsonBody =  "{\"secondaryFields\":[],\"entityType\":\"INDIVIDUAL\",\"customFields\":[],\"groupId\":\"VVVVVVVVVVVVVVV-KKKKK-LLLLLLLLLL",\"providerTypes\":[\"WATCHLIST\"],\"name\":\"george bush\"}";
            
            // create a JSON object from the JSON string
            JSONObject jo = new JSONObject(jsonBody);

            //System.out.println(jo.toString());

            String jlen = String.valueOf(jo.toString().length());
            String dataToSign = "(request-target): post " + gatewayurl + "cases\n" +
                    "host: " + gatewayhost + "\n" +
                    "date: " + date + "\n" +
                    "content-type: " +  "application/json" +"\n" + 
                    "content-length: " + jlen + "\n" + 
                    jo;
            
            String hmac = generateAuthHeader(dataToSign, apisecret);
            String authorisation = "Signature keyId=\"" + apikey  + "\",algorithm=\"hmac-sha256\",headers=\"(request-target) host date content-type content-length\",signature=\"" + hmac + "\"";
            
            System.out.println(jlen);
            System.out.println(dataToSign);
            //System.out.println(hmac);
            System.out.println(authorisation);
            String msg = jo.toString();
            
            //HttpPost httpPost = new HttpPost("https://rms-world-check-one-api-pilot.thomsonreuters.com/v1/cases");
            //HttpPost httpPost = new HttpPost("https://rms-world-check-one-api-pilot.thomsonreuters.com/v2/cases");
            HttpPost httpPost = new HttpPost("https://api-worldcheck.refinitiv.com/v2/groups");  

            HttpEntity entity = new StringEntity(msg);
            httpPost.setEntity(entity);
            
            httpPost.addHeader("Date", date);
            httpPost.addHeader("Cache-Control", "no-cache");         
            httpPost.addHeader("Content-Type",  "application/json" );
            httpPost.addHeader("Authorization", authorisation);        
            
            // send the POST request
            CloseableHttpResponse response1 = httpclient.execute(httpPost);

            try {
            
                HttpEntity entity1 = response1.getEntity();
                System.out.println(response1.getStatusLine());
                
                String json = EntityUtils.toString(response1.getEntity());
                System.out.println(entity1);
                // json string returned
                System.out.println(json);
                ObjectMapper mapper = new ObjectMapper();
                
                // printout in Pretty format
                Object jsonObj = mapper.readValue(json, Object.class);
                String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObj);
                System.out.println(indented);

                EntityUtils.consume(entity1);
                
            } 
            finally {
                response1.close();
            }


        } finally {
            httpclient.close();
        }
    }
}

I got this exception:

Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
 at [Source: (String)""; line: 1, column: 0]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
    at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4765)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4667)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3629)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3597)
    at com.mycompany.post1.Post1.main(Post1.java:116)
Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
    at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404)
    at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166)
    at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:982)
    at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:929)
    at org.codehaus.mojo.exec.ExecMojo.execute (ExecMojo.java:457)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:301)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:211)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:165)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:157)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:121)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:127)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:294)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:960)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:104)
    at java.lang.reflect.Method.invoke (Method.java:577)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time:  3.734 s
Finished at: 2022-08-08T21:37:25+06:00
------------------------------------------------------------------------
Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) on project post1: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

pom file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>post1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>18</maven.compiler.source>
        <maven.compiler.target>18</maven.compiler.target>
        <exec.mainClass>com.mycompany.post1.Post1</exec.mainClass>
    </properties>
    
    <dependencies>
    <dependency>
        <groupId>com.cedarsoftware</groupId>
        <artifactId>java-util</artifactId>
        <version>1.8.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.13.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.15</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.13</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.json/json -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20220320</version>
    </dependency>
    </dependencies>
</project>

Could you please tell, what I have done incorrect. Netbeans details: Product Version: Apache NetBeans IDE 14 Java: 18.0.2; Java HotSpot(TM) 64-Bit Server VM 18.0.2+9-61 Runtime: Java(TM) SE Runtime Environment 18.0.2+9-61 System: Windows 10 version 10.0 running on amd64; UTF-8; ru_RU (nb) User directory: C:\Users\SAPPI_WorkBech\AppData\Roaming\NetBeans\14 Cache directory: C:\Users\SAPPI_WorkBech\AppData\Local\NetBeans\Cache\14

0 Answers
Related