in POST call whatever request body is coming should be written in test file in java

146 Views Asked by At

I want to write POST call with plain/text data type consumption, which will read request and write to a file. Using below code it write in file only one line not complete body. Any suggestion.

Handler class

@Path("store/{storeId}/datareceive")
@Description("Receives data from NiFi")
public class ElasticSearchDataRequestHandler extends AbstractConfigBasedClassicHandler {

    @Override
    public String getResourceName() {
        return RESOURCE_NAME;
    }

    @POST
    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML, MediaType.APPLICATION_XHTML_XML, MediaType.APPLICATION_ATOM_XML })
    public Response addNiFiDataToFile(@PathParam(STORE_ID) String storeId) throws Exception {
        final String methodName = "addNiFiDataToFile";
        LOGGER.debug(CLASSNAME, methodName);

        TypedProperty requestProperties = new TypedProperty();
        

        HashMap requstJson = new HashMap();
        
        System.out.println("request ---->" + request);

        requstJson.putAll(getMapFromRequest(request, MediaType.TEXT_PLAIN));

        
        System.out.println("-----requstJson-----" + requstJson);
        requestProperties.put("requstJson", requstJson);
        return executeControllerCommandWithContext(storeId, ElasticSearchDataRequestCmd.NAME, requestProperties, MediaType.APPLICATION_JSON);
    }
}

Command


//private TypedProperty reqProp;
    private Object esData;
    //private TypedProperty respProperty;

    @Override
    public void setRequestProperties(TypedProperty reqProperties) throws ECException {
        if (reqProperties != null) {
            esData = reqProperties.get("requstJson");
            //System.out.println("reqProp =====" + reqProp);

        }
    }


public void performExecute() throws ECException {
    if (Objects.isNull(getResponseProperties()) || !getResponseProperties().containsKey(LogicConstants.CODE)) {
        addDataToFile();
    }
}

private void addDataToFile() throws ECException {
    System.out.println("----------------------Test Data--------------------------" + esData);
    
    TypedProperty respProperty = new TypedProperty();
    try {
        String TEXT_FILE = "C:" + File.separator + "Users" + File.separator + "pawankumar" + File.separator + "Documents" + File.separator + getFileWithDate("product") + ".txt";
        // Use relative path for Unix systems
        System.out.println("TEXT_FILE ---------------> " + TEXT_FILE);
        Path textFilePath = Paths.get(TEXT_FILE);
        Files.createFile(textFilePath);
        FileWriter myWriter = new FileWriter(TEXT_FILE);
        System.out.println("===================" + esData);
        //myWriter.write(reqProp);
        myWriter.write(esData.toString());
        myWriter.close();

        System.out.println("Successfully wrote to the file.");

        
        respProperty.put(LogicConstants.CODE, String.valueOf(HttpStatus.OK.getCode()));
        respProperty.put(LogicConstants.MESSAGE, "Data added to file");

    } catch (IOException e) {
        respProperty.put(LogicConstants.CODE, String.valueOf(HttpStatus.OK.getCode()));
        respProperty.put(LogicConstants.MESSAGE, "Data added to file");
        e.printStackTrace();
    }
    /*TypedProperty respProperty = new TypedProperty();
    respProperty.put(LogicConstants.CODE, String.valueOf(HttpStatus.OK.getCode()));
    respProperty.put(LogicConstants.MESSAGE, "User is successfully subscribed for newsletter.");*/

    setResponseProperties(respProperty);
}

To test POST : http://localhost/wcs/resources/store/10151/datareceive

Body : plain/text

Request is as below:

{ "update": { "_id": "10151--1-10001-3074457345616676668", "_index": ".auth.10151.product.202206290450", "retry_on_conflict": 5, "_source": false } }
{ "doc": {"attachments":[{"identifier":"0AN4268__26346G_9cefa68b-1993-4294-bcd9-aafe0116c66f/0AN4268__26346G_090A","sequence":34.0,"name.raw":"Degree_090","description.raw":"","mime":"","rule":"PDP","id":"3074457345616669358","store":"10051","url":"/cdn-record-files-pi/ea504707-a6a6-4b6b-bb18-aaf701016153/9cefa68b-1993-4294-bcd9-aafe0116c66f/0AN4268__26346G_090A.png"},{"identifier":"0AN4268__26346G_d4279fe2-c87a-473b-909a-aafe0116d302/0AN4268__26346G_180A","sequence":35.0,"name.raw":"Degree_180","description.raw":"","mime":"","rule":"PDP","id":"3074457345616669359","store":"10051","url":"/cdn-record-files-pi/ea504707-a6a6-4b6b-bb18-aaf701016153/d4279fe2-c87a-473b-909a-aafe0116d302/0AN4268__26346G_180A.png"},{"identifier":"0AN4268__26346G_3f2f5455-651d-4558-9fc9-aafe0116be65/0AN4268__26346G_030A","sequence":32.0,"name.raw":"Degree_030","description.raw":"","mime":"","rule":"PDP","id":"3074457345616669360","store":"10051","url":"/cdn-record-files-pi/ea504707-a6a6-4b6b-bb18-aaf701016153/3f2f5455-651d-4558-9fc9-aafe0116be65/0AN4268__26346G_030A.png"},{"identifier":"0AN4268__26346G_3f2f5455-651d-4558-9fc9-aafe0116be65/0AN4268__26346G_030A_PLP","sequence":2.0,"name.raw":"Degree_030","description.raw":"","mime":"","rule":"PLP","id":"3074457345616669361","store":"10051","url":"/cdn-record-files-pi/ea504707-a6a6-4b6b-bb18-aaf701016153/3f2f5455-651d-4558-9fc9-aafe0116be65/0AN4268__26346G_030A.png"},{"identifier":"0AN4268__26346G_3f2f5455-651d-4558-9fc9-aafe0116be65/0AN4268__26346G_030A_Thumbnail","sequence":1.0,"name.raw":"Degree_030","description.raw":"","mime":"","rule":"Thumbnail","id":"3074457345616669362","store":"10051","url":"/cdn-record-files-pi/ea504707-a6a6-4b6b-bb18-aaf701016153/3f2f5455-651d-4558-9fc9-aafe0116be65/0AN4268__26346G_030A.png"},{"identifier":"0AN4268__26346G_9dd059c7-a0fc-434e-8594-aafe0116ba86/0AN4268__26346G_000A","sequence":33.0,"name.raw":"Degree_000","description.raw":"","mime":"","rule":"PDP","id":"3074457345616669363","store":"10051","url":"/cdn-record-files-pi/ea504707-a6a6-4b6b-bb18-aaf701016153/9dd059c7-a0fc-434e-8594-aafe0116ba86/0AN4268__26346G_000A.png"},{"identifier":"0AN4268__26346G_9dd059c7-a0fc-434e-8594-aafe0116ba86/0AN4268__26346G_000A_PLP","sequence":1.0,"name.raw":"Degree_000","description.raw":"","mime":"","rule":"PLP","id":"3074457345616669364","store":"10051","url":"/cdn-record-files-pi/ea504707-a6a6-4b6b-bb18-aaf701016153/9dd059c7-a0fc-434e-8594-aafe0116ba86/0AN4268__26346G_000A.png"}],"__meta":{"modified":"2022-06-29T04:51:36.939Z"}}, "doc_as_upsert": true }
0

There are 0 best solutions below