Java Upload file Rest Template - java

I need to upload a file through the spring MVC rest template for the below params shown in the image.
I have tried different approaches but it's not working giving exception 401 Unauthorized.
I need code that uploads files through spring MVC RestTemplate.
I have shared my code below which is not working.I have tried different ways to attach file but they are not working please check my code.
My Code Sample:
#RequestMapping(value = "/uploadPEFile", method = { RequestMethod.POST},
produces = "application/json")
#ResponseBody
public String uploadPEFile(#RequestParam(value = "file", required = false) MultipartFile file,
HttpServletRequest request, HttpServletResponse httpResponse) {
JsonResponse objResponse = null;
String enterpriseId = "";
String enterpriseName = "";
String type = "";
String productId = "";
String userIds = "";
String title = "";
String description = "";
String date = "";
String externalUrl = "";
String presenterName = "";
try {
enterpriseId = request.getParameter("enterpriseId");
enterpriseName = request.getParameter("enterpriseName");
type = request.getParameter("type");
productId = request.getParameter("productId");
userIds = request.getParameter("userIds");
title = request.getParameter("title");
description = request.getParameter("description");
date = request.getParameter("date");
if(date != null && !date.isEmpty())
{
String [] dateArray = date.split(" ");
date = dateArray[2]+" "+getMonth(dateArray[1])+", "+dateArray[3];
}
externalUrl = request.getParameter("externalURL");
presenterName = request.getParameter("presenterName");
JsonObject jsonBody = new JsonObject();
jsonBody.addProperty("title", title);
jsonBody.addProperty("description", description);
jsonBody.addProperty("date", date);
jsonBody.addProperty("external_url", externalUrl);
jsonBody.addProperty("presenter_name", presenterName);
String cmsDeviceId = PortalUtil.getPropertyValue("cms.device.id");
if (file != null) {
final String filename = file.getOriginalFilename();
ByteArrayResource fileAsResource = new ByteArrayResource(file.getBytes()) {
#Override
public String getFilename() {
return filename;
}
};
String filePath = "/persivia edge/"+type+"/"+filename;
File userFile = new File(System.getProperty(PortalConstants.cdnPath) + filePath);
if(!userFile.exists()) {
userFile.mkdirs();
}
file.transferTo(userFile);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("device_id",cmsDeviceId);
// body.add("attachment",fileAsResource);
// body.add("attachment",new MultipartInputStreamFileResource(file.getInputStream(), file.getOriginalFilename()));
body.add("attachment",new FileSystemResource(filePath));
body.add("source", "web");
body.add("filename", filename);
body.add("enterprise_id", enterpriseId);
body.add("enterpriseName", enterpriseName);
body.add("type", type);
body.add("product_id",productId);
body.add("userIds",userIds);
body.add("body", jsonBody.toString());
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
RestTemplate restTemplate = new RestTemplate();
String requestUrl = PortalUtil.getPropertyValue("cms.api.url")+"wp-json/wp/v2/upload_content";
String response = restTemplate.postForObject(requestUrl, requestEntity, String.class);
httpResponse.setStatus(HttpServletResponse.SC_OK);
JsonObject data = new JsonObject();
objResponse = JsonResponse.build(ResponseStatus.status200, ResponseStatus.message200,
"Logo info updated successfully", data);
}
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage(), e.getCause());
objResponse = JsonResponse.build(ResponseStatus.status500, ResponseStatus.message500,
e.getMessage(), null);
httpResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
return new Gson().toJson(objResponse);
}

Related

Post HTTP call working on postman but not on local in Java

I am calling an api the purpose of which to validate customer's data which is working fine in postman but in my local while doing http call it's giving a different response and not passing even the first line. The request and response are in the form of XML.
Postman request and response are as follows:
curl --location --request POST 'https://ckyc.olacabs-dev.in:54322/crmapi/TrackwizzWebApi/A63/CreateUpdateCustomer/V1' \
--header 'Content-Type: text/plain' \
--data-raw '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CustomerRelatedPartyCreateUpdateRequest>
<ParentCompany>NFPL</ParentCompany>
<ApiToken>ecef5041-a08a-43c9-9599-fc2d0c8da616</ApiToken>
<RecordRequestDetails>
<RecordRequestDetail>
<SourceSystemName>HomeLoan</SourceSystemName>
<SourceSystemCustomerCode>321875112</SourceSystemCustomerCode>
<IsSmallCustomer>0</IsSmallCustomer>
<EkycOTPBased>0</EkycOTPBased>
<CustomerStatus>
<StatusDetail>
<Status>Active</Status>
<EffectiveDate>1970-01-01T05:29:57.991+05:30</EffectiveDate>
</StatusDetail>
</CustomerStatus>
<CustomerType>1</CustomerType>
<Prefix>MISS</Prefix>
<FirstName>dolly</FirstName>
<LastName>vaishnav</LastName>
<FatherPrefix>MR</FatherPrefix>
<fatherFirstName>Prakash</fatherFirstName>
<Gender>female</Gender>
<formSixty>0</formSixty>
<KYCAttestationType>1</KYCAttestationType>
<CountryOfResidence>IN</CountryOfResidence>
<DateofBirth>1970-01-01T05:29:57.991+05:30</DateofBirth>
<KYCDateOfDeclaration>1970-01-01T05:29:57.991+05:30</KYCDateOfDeclaration>
<KYCPlaceOfDeclaration>Mumbai</KYCPlaceOfDeclaration>
<KYCVerificationDate>1970-01-01T05:29:57.991+05:30</KYCVerificationDate>
<KYCEmployeeName>Vikash</KYCEmployeeName>
<KYCEmployeeDesignation>manager</KYCEmployeeDesignation>
<KYCVerificationBranch>mumbai</KYCVerificationBranch>
<KYCEmployeeCode>1234</KYCEmployeeCode>
<AddressDetails>
<AddressDetail>
<AddressType>permanent</AddressType>
<AddressLine1>xyz</AddressLine1>
<PinCode>491001</PinCode>
<City>bhilai</City>
<AddressLine2>kormangala</AddressLine2>
<AddressLine3>5th block</AddressLine3>
<State>CG</State>
<Country>IN</Country>
<District>durg</District>
</AddressDetail>
</AddressDetails>
<PermanentAddressProof>passport</PermanentAddressProof>
<CorrespondenceAddressProof>dl</CorrespondenceAddressProof>
<IdentificationDetails>
<IdentificationDetail>
<IdType>passport</IdType>
<IdNumber>AVPBV4738</IdNumber>
</IdentificationDetail>
</IdentificationDetails>
<Minor>0</Minor>
<ModuleApplicable>
<ModuleDetail>
<Module>ckyc</Module>
</ModuleDetail>
</ModuleApplicable>
</RecordRequestDetail>
</RecordRequestDetails>
</CustomerRelatedPartyCreateUpdateRequest>
'
Response:
<?xml version="1.0" encoding="utf-16"?>
<A63CustomerResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ApiToken>ecef5041-a08a-43c9-9599-fc2d0c8da616</ApiToken>
<RequestId>82718d83-9206-4ab1-819a-a79e63e832a4</RequestId>
<RequestStatus>AcceptedbyTW</RequestStatus>
<ParentCompany>NFPL</ParentCompany>
<CustomerResponseDetails>
<A63CustomerResponseDetail>
<TransactionId xsi:nil="true" />
<SourceSystemName>HomeLoan</SourceSystemName>
<SourceSystemCustomerCode>321875112</SourceSystemCustomerCode>
<CustomerIntegrationStatusCode>AcceptedbyTW</CustomerIntegrationStatusCode>
<TrackWizzUpdatedOn>22-02-2022</TrackWizzUpdatedOn>
</A63CustomerResponseDetail>
</CustomerResponseDetails>
</A63CustomerResponse>
Sample java code:
public Map<String, String> updateCustomerData(String userId, CkycCustomerDetailsUpdateRequest request) throws CkycException {
String addressType = request.getAddressType();
String addressLine1 = request.getAddressLine1();
String addressLine2 = request.getAddressLine2();
String addressLine3 = request.getAddressLine3();
int pincode = request.getPincode();
String city = request.getCity();
String state = request.getState();
String country = request.getCountry();
String district = request.getDistrict();
AddressDetail addressDetail = formPayloadForAddressDetail(addressType, addressLine1, addressLine2, addressLine3, pincode, city, state, country, district);
List<AddressDetail> addressDetailList = new ArrayList<>();
addressDetailList.add(addressDetail);
AddressDetails addressDetails = new AddressDetails();
addressDetails.setAddressDetail(addressDetailList);
String idType = request.getIdType();
String idNumber = request.getIdNumber();
IdentificationDetail identificationDetail = formPayloadForIdentificationDetail(idType, idNumber);
IdentificationDetails identificationDetails = new IdentificationDetails();
identificationDetails.setIdentificationDetail(identificationDetail);
String module = request.getModule();
ModuleDetail moduleDetail = new ModuleDetail();
moduleDetail.setModule(module);
ModuleApplicable moduleApplicable = new ModuleApplicable();
moduleApplicable.setModuleDetail(moduleDetail);
String status = request.getStatus();
Date effectiveDate = request.getEffectiveDate();
CustomerStatus customerStatus = formPayloadForCustomerStatus(status, effectiveDate);
String prefix = request.getPrefix();
String firstName = request.getFirstName();
String lastName = request.getLastName();
String fatherPrefix = request.getFatherPrefix();
String fatherFirstName = request.getFatherFirstName();
String gender = request.getGender();
String sourceSystemName = request.getSourceSystemName();
String sourceSystemCustomerCode = request.getSourceSystemCustomerCode();
int isSmallCustomer = request.getIsSmallCustomer();
int ekycOTPBased = request.getEkycOTPBased();
String customerType = request.getCustomerType();
int formSixty = request.getFormSixty();
int kycAttestationType = request.getKYCAttestationType();
String countryOfResidence = request.getCountryOfResidence();
Date dob = request.getDob();
Date kycDateOfDeclaration = request.getKycDateOfDeclaration();
String kycPlaceOfDeclaration = request.getKycPlaceOfDeclaration();
Date kycVerificationDate = request.getKycVerificationDate();
String kycEmployeeName = request.getKycEmployeeName();
String kycEmployeeDesignation = request.getKycEmployeeDesignation();
String kycVerificationBranch = request.getKycVerificationBranch();
String kycEmployeeCode = request.getKycEmployeeCode();
String permanentAddressProof = request.getPermanentAddressProof();
String correspondenceAddressProof = request.getCorrespondenceAddressProof();
int minor = request.getMinor();
RecordRequestDetails recordRequestDetails = formPayloadForRecordRequestDetails(prefix, firstName, lastName, fatherFirstName, fatherPrefix, gender, sourceSystemCustomerCode, sourceSystemName,
isSmallCustomer, ekycOTPBased, customerType, formSixty, countryOfResidence, dob, kycAttestationType, kycDateOfDeclaration, kycPlaceOfDeclaration,
kycVerificationBranch, kycVerificationDate, kycEmployeeName, kycEmployeeDesignation, kycEmployeeCode, permanentAddressProof, correspondenceAddressProof, minor,
customerStatus, identificationDetails, moduleApplicable, addressDetails);
String parentCompany = request.getParentCompany();
String apiToken = request.getApiToken();
A63CustomerDetailsUpdateRequest a63CustomerDetailsUpdateRequest = formPayloadForCustomerDetailsUpdateReqest(parentCompany, apiToken, recordRequestDetails);
String payloadCustomerUpdateDetailsXml = XMLUtil.toXML(a63CustomerDetailsUpdateRequest);
String response = null;
response = doPostCkycReverseUpload(userId, payloadCustomerUpdateDetailsXml, trackwizzConfig.getImageIntegrationEndPoint(), 0, CKYC_UPLOAD, CUSTOMER_VALIDATION_API);
JSONObject jsonObject = XML.toJSONObject(response, true);
String jsonString = jsonObject.toString();
try {
return mapper.readValue(jsonString, Map.class);
} catch (IOException e) {
throw new CkycException(ErrorCode.internal_parsing_error, "Unable to parse the response");
}
}
private A63CustomerDetailsUpdateRequest formPayloadForCustomerDetailsUpdateReqest(String parentCompany, String apiToken, RecordRequestDetails recordRequestDetails) {
A63CustomerDetailsUpdateRequest a63CustomerDetailsUpdateRequest = new A63CustomerDetailsUpdateRequest();
a63CustomerDetailsUpdateRequest.setParentCompany(parentCompany);
a63CustomerDetailsUpdateRequest.setApiToken(apiToken);
a63CustomerDetailsUpdateRequest.setRecordRequestDetails(recordRequestDetails);
return a63CustomerDetailsUpdateRequest;
}
private RecordRequestDetails formPayloadForRecordRequestDetails(String prefix, String firstName, String lastName, String fatherFirstName, String fatherPrefix, String gender, String sourceSystemCustomerCode, String sourceSystemName, int isSmallCustomer, int ekycOTPBased, String customerType, int formSixty, String countryOfResidence, Date dob, int kycAttestationType, Date kycDateOfDeclaration, String kycPlaceOfDeclaration, String kycVerificationBranch, Date kycVerificationDate, String kycEmployeeName, String kycEmployeeDesignation, String kycEmployeeCode, String permanentAddressProof, String correspondenceAddressProof, int minor, CustomerStatus customerStatus, IdentificationDetails identificationDetails, ModuleApplicable moduleApplicable, AddressDetails addressDetails) {
RecordRequestDetail recordRequestDetail = new RecordRequestDetail();
recordRequestDetail.setPrefix(prefix);
recordRequestDetail.setFirstName(firstName);
recordRequestDetail.setLastName(lastName);
recordRequestDetail.setFatherPrefix(fatherPrefix);
recordRequestDetail.setFatherFirstName(fatherFirstName);
recordRequestDetail.setGender(gender);
recordRequestDetail.setSourceSystemName(sourceSystemName);
recordRequestDetail.setSourceSystemCustomerCode(sourceSystemCustomerCode);
recordRequestDetail.setIsSmallCustomer(isSmallCustomer);
recordRequestDetail.setEkycOTPBased(ekycOTPBased);
recordRequestDetail.setCustomerType(customerType);
recordRequestDetail.setCustomerStatus(customerStatus);
recordRequestDetail.setFormSixty(formSixty);
recordRequestDetail.setDob(dob);
recordRequestDetail.setMinor(minor);
recordRequestDetail.setCountryOfResidence(countryOfResidence);
recordRequestDetail.setKycAttestationType(kycAttestationType);
recordRequestDetail.setKycDateOfDeclaration(kycDateOfDeclaration);
recordRequestDetail.setKycEmployeeCode(kycEmployeeCode);
recordRequestDetail.setKycEmployeeName(kycEmployeeName);
recordRequestDetail.setKycPlaceOfDeclaration(kycPlaceOfDeclaration);
recordRequestDetail.setKycVerificationBranch(kycVerificationBranch);
recordRequestDetail.setKycVerificationDate(kycVerificationDate);
recordRequestDetail.setKycEmployeeDesignation(kycEmployeeDesignation);
recordRequestDetail.setPermanentAddressProof(permanentAddressProof);
recordRequestDetail.setCorrespondenceAddressProof(correspondenceAddressProof);
recordRequestDetail.setIdentificationDetails(identificationDetails);
recordRequestDetail.setModuleApplicable(moduleApplicable);
recordRequestDetail.setAddressDetails(addressDetails);
RecordRequestDetails recordRequestDetails = new RecordRequestDetails();
recordRequestDetails.setRecordRequestDetail(recordRequestDetail);
return recordRequestDetails;
}
private CustomerStatus formPayloadForCustomerStatus(String status, Date effectiveDate) {
StatusDetail statusDetail = new StatusDetail();
statusDetail.setStatus(status);
statusDetail.setEffectiveDate(effectiveDate);
CustomerStatus customerStatus = new CustomerStatus();
customerStatus.setStatusDetail(statusDetail);
return customerStatus;
}
private IdentificationDetail formPayloadForIdentificationDetail(String idType, String idNumber) {
IdentificationDetail identificationDetail = new IdentificationDetail();
identificationDetail.setIdNumber(idNumber);
identificationDetail.setIdType(idType);
return identificationDetail;
}
private AddressDetail formPayloadForAddressDetail(String addressType, String addressLine1, String addressLine2, String addressLine3, int pincode, String city, String state, String country, String district) {
AddressDetail addressDetail = new AddressDetail();
addressDetail.setAddressType(addressType);
addressDetail.setAddressLine1(addressLine1);
addressDetail.setAddressLine2(addressLine2);
addressDetail.setAddressLine3(addressLine3);
addressDetail.setPinCode(pincode);
addressDetail.setCity(city);
addressDetail.setState(state);
addressDetail.setCountry(country);
addressDetail.setDistrict(district);
return addressDetail;
}
private void setHeadersAsXml(AbstractHttpMessage entity) {
entity.setHeader(CONTENT_TYPE, APPLICATION_XML);
}
#Trace(dispatcher = true)
private String doPostCkycReverseUpload(String userId, String request, String endPoint, int retryCount, String context, String tssApi) throws CkycException {
HttpPost post = new HttpPost(trackwizzConfig.getBaseUrl() + endPoint);
setEntity(post, request);
setHeaders(post);
post.setConfig(requestConfig);
long startTime = System.currentTimeMillis();
long endTime = 0;
try (CloseableHttpResponse response = httpClient.execute(post)){
endTime = System.currentTimeMillis();
log.info("[TSS] Response from trackwizz : {} with status : {}", response, response.getStatusLine().getStatusCode());
int status = response.getStatusLine().getStatusCode();
NewRelic.addCustomParameter(tssApi+HYPHEN+STATUS, String.valueOf(status));
NewRelic.addCustomParameter(tssApi+LATENCY, (endTime - startTime)/1000);
if(status == 200){
log.info("[TSS] Successfully received response from trackwizz service for integration of the " + tssApi);
return EntityUtils.toString(response.getEntity());
} else{
log.info("Exception occured while integrating the " + tssApi);
throw new Exception();
}
} catch (SocketTimeoutException | ConnectTimeoutException ex) {
endTime = System.currentTimeMillis();
NewRelic.addCustomParameter(tssApi+HYPHEN+STATUS, "408");
NewRelic.addCustomParameter(tssApi+LATENCY, (endTime - startTime)/1000);
if(retryCount < trackwizzConfig.getMaxRetryCount()){
log.info("[TSS]Error while receiving response from trackwizz service. Failed by timeout exception, retryCount : {}", retryCount);
return doPostCkycReverseUpload(userId, request, endPoint, retryCount+1, context, tssApi);
}
log.info("[TSS]Error while receiving response from trackwizz service. Failed by timeout exception, retryCount : {}", retryCount);
throw new CkycException(ErrorCode.unknown_error, "Error while receiving response from trackwizz service. Failed by timeout exception, retry count:" + retryCount);
} catch (Exception e) {
NewRelic.addCustomParameter(tssApi+HYPHEN+STATUS, "NA");
log.error("[TSS]Exception : {} for request : {}", e.getMessage(), request);
throw new CkycException(ErrorCode.unknown_error, e.getMessage());
}
}
}
XMLUtil class where I am performing unmarshalling:
package com.olacabs.money.utils;
import lombok.extern.slf4j.Slf4j;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
#Slf4j
public class XMLUtil {
public static String toXML(Object data) {
String xml = "";
try {
log.info("Generating xml for: " + data.getClass());
JAXBContext jaxbContext = JAXBContext.newInstance(data.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(data, sw);
xml = sw.toString();
} catch (JAXBException e) {
log.error("Exception {} while converting data to xml", e.getMessage(), e);
}
return xml;
}
}
Response I am getting from the class:
<?xml version="1.0" encoding="utf-16"?>
<A63CustomerResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ApiToken>ecef5041-a08a-43c9-9599-fc2d0c8da616</ApiToken>
<RequestId>b9393295-cf97-49c4-8c0f-978e68b1dd2e</RequestId>
<RequestStatus>RejectedByTW</RequestStatus>
<RequestRejectionCode>RC184</RequestRejectionCode>
<RequestRejectionDescription>Invalid API Token, API token does not exist in TrackWizz.</RequestRejectionDescription>
<ParentCompany>NFPL</ParentCompany>
<CustomerResponseDetails />
</A63CustomerResponse>
The api token which I am passing in both the cases are same only.
Maven dependency added:
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.8</version>
</dependency>
Not posting the sample request body as it's huge.

spring mvc outputStream csv

How to split de code into Service, repository and controller (Spring mvc)
this code work for me but not good split.
Big table large table to expose API REST
#ApiOperation(value = "Search all customers",response = GoldenCustomer.class)
#RequestMapping(value = "/goldenall/",method = RequestMethod.GET, produces = "application/json")
#ResponseBody
public ResponseEntity<?> getAllCustomers(HttpServletResponse response){
LOGGER.info("Get all Golden Customers...");
List<GoldenCustomer> goldenCustomers = null;
String message = "Unknown exception";
try {
PreparedStatement st = session.prepare("select * from ckm_d_pilot.golden_clusters_by_customer where solr_query=?");
//TODO
String input_string = "*";
String solrQuery = "{\"q\": \"*:"+input_string+"\", \"paging\": \"driver\"}";
BoundStatement boundSt = st.bind(solrQuery);
ResultSet rs = session.execute(boundSt.setFetchSize(1000));
response.setContentType("text/csv");
response.setHeader("Content-Disposition", "attachment; filename=export.csv;");
ServletOutputStream os = response.getOutputStream();
for(Row row: rs){
String csvLine = row.getString("cluster_id")+","+row.getString("src_customer_id")+"\n";
os.write(csvLine.getBytes("UTF-8"));
}
response.flushBuffer();
}catch (Exception e){
LOGGER.error("Intetrnal error WebService exception not found "+ message, e);
throw new ReportInternalException(message);
}
return new ResponseEntity<>(HttpStatus.OK);
}
Here is the minimum refactored code which gives you some information on how it can be done.
#ApiOperation(value = "Search all customers",response = GoldenCustomer.class)
#RequestMapping(value = "/goldenall/",method = RequestMethod.GET, produces = "application/json")
#ResponseBody
public ResponseEntity<?> getAllCustomers(HttpServletResponse response){
LOGGER.info("Get all Golden Customers...");
List<GoldenCustomer> goldenCustomers = null;
String message = "Unknown exception";
try {
String custCsv = new CustomerServiceImpl().getAllCustomers();
response.setContentType("text/csv");
response.setHeader("Content-Disposition", "attachment; filename=export.csv;");
ServletOutputStream os = response.getOutputStream();
os.write(custCsv.getBytes("UTF-8"));
response.flushBuffer();
}catch (Exception e){
LOGGER.error("Intetrnal error WebService exception not found "+ message, e);
throw new ReportInternalException(message);
}
return new ResponseEntity<>(HttpStatus.OK);
}
public class CustomerServiceImpl {
public String getAllCustomers () {
return new CustomersDAOImpl().getAllCustomers();
}
}
public class CustomersDAOImpl() {
public String getAllCustomers() {
StringBuilder sb = new StringBuilder();
PreparedStatement st = session.prepare("select * from ckm_d_pilot.golden_clusters_by_customer where solr_query=?");
//TODO
String input_string = "*";
String solrQuery = "{\"q\": \"*:"+input_string+"\", \"paging\": \"driver\"}";
BoundStatement boundSt = st.bind(solrQuery);
ResultSet rs = session.execute(boundSt.setFetchSize(1000));
for(Row row: rs){
String csvLine = row.getString("cluster_id")+","+row.getString("src_customer_id")+"\n";
sb.append(csvLine);
}
return sb.toString();
}
}
Note:- I have not tested the below code

how to post data in key/value pair? [duplicate]

This question already has answers here:
Java - sending HTTP parameters via POST method easily
(18 answers)
Closed 5 years ago.
i need to post data to particular url
in which in content i need to post html in content array and in meta headers in json format.
URL oracle = new URL("");
try (BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()))) {
String inputLine1;
while ((inputLine1 = in.readLine()) != null) {
System.out.println(inputLine1);
com.eclipsesource.json.JsonObject object = Json.parse(inputLine1).asObject();
com.eclipsesource.json.JsonArray items = Json.parse(inputLine1).asObject().get("data").asArray();
for (JsonValue item : items) {
//System.out.println(item.toString());
String name = item.asObject().getString("id", "Unknown Item");
System.out.println(name);
String quantity = item.asObject().getString("url", "id");
// JSONArray jsonArray2 = new JSONArray(quantity);
System.out.println(quantity);
/* Platform.runLater(() ->{
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Logger.getLogger(HV1.class.getName()).log(Level.SEVERE, null, ex);
}*/
Img.load(quantity);
URL url;
InputStream is = null;
BufferedReader br;
String line;
url = new URL(quantity);
is = url.openStream(); // throws an IOException
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
System.out.println(line);
byte[] postData= line.getBytes( StandardCharsets.UTF_8 );
wb2.load(line);
String originalUrl = "";
String newUrl = originalUrl.replace("ID", name);
System.out.println(newUrl);
String request = newUrl;
URL url1 = new URL( request );
HttpURLConnection conn= (HttpURLConnection) url1.openConnection();
conn.setDoOutput( true );
conn.setInstanceFollowRedirects( false );
conn.setRequestMethod( "POST" );
conn.setRequestProperty( "Content-Type", "text/plain");
conn.setRequestProperty( "charset", "utf-8");
//conn.setRequestProperty( "Content-Length", Integer.toString( line ));
conn.setUseCaches( false );
try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
wr.write(postData);
System.out.println("200 ok");
this is what i tried but i had post in text/plain but i want to post in key/value pair.
updated code
URL oracle = new URL("");
try (BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()))) {
String inputLine1;
while ((inputLine1 = in.readLine()) != null) {
System.out.println(inputLine1);
com.eclipsesource.json.JsonObject object = Json.parse(inputLine1).asObject();
com.eclipsesource.json.JsonArray items = Json.parse(inputLine1).asObject().get("data").asArray();
for (JsonValue item : items) {
//System.out.println(item.toString());
String name = item.asObject().getString("id", "Unknown Item");
System.out.println(name);
String quantity = item.asObject().getString("url", "id");
// JSONArray jsonArray2 = new JSONArray(quantity);
System.out.println(quantity);
/* Platform.runLater(() ->{
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Logger.getLogger(HV1.class.getName()).log(Level.SEVERE, null, ex);
}*/
Img.load(quantity);
URL url;
InputStream is = null;
BufferedReader br;
String line;
url = new URL(quantity);
is = url.openStream(); // throws an IOException
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
System.out.println(line);
byte[] postData= line.getBytes( StandardCharsets.UTF_8 );
wb2.load(line);
String originalUrl = "";
String newUrl = originalUrl.replace("ID", name);
System.out.println(newUrl);
URL url1 = new URL(newUrl);
Map<String,Object> params = new LinkedHashMap<>();
params.put("content", postData);
params.put("meta", "abc");
StringBuilder postData1 = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
if (postData1.length() != 0) postData1.append('&');
postData1.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData1.append('=');
postData1.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData1.toString().getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection)url1.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in1 = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
for (int c; (c = in1.read()) >= 0;)
System.out.print((char)c);
/* try{
Thread.sleep(400);
}catch(InterruptedException e){System.out.println(e);} */
}
}
}
this is my updted code(answer) this is how i solve my problem thanks for your precious time.
Take a look at this previous answer regarding HTTP Post parameters that exploit BasicNameValuePairs.
Name Value Pairs
Here is a pertinent piece of code from that answer.
HttpClient httpclient;
HttpPost httppost;
ArrayList<NameValuePair> postParameters;
httpclient = new DefaultHttpClient();
httppost = new HttpPost("your login link");
postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("param1", "param1_value"));
postParameters.add(new BasicNameValuePair("param2", "param2_value"));
httpPost.setEntity(new UrlEncodedFormEntity(postParameters, "UTF-8"));
HttpResponse response = httpclient.execute(httpPost);
Best would be using something like Spring and Jackson to create a JSON sending via a request, if you are not familiar with what you are trying to achieve:
This is just basic implementation
private final String uri = "yoururl.de/asdfasd";
private final HttpMethod httpMethod = HttpMethod.POST;
private final ContentType contentType = ContentType.json;
And EPO to transfer the Data
SendKeyValuePairsEPO implements Serializable{
private static final long serialVersionUID = 5311348008314829094L;
private final Integer startIndex;
private final Integer size;
private final Integer totalSize;
private final List<KeyValuePairEPO> values;
/**
* Contructor
*
* #param startIndex start searching index
* #param size requested result size
* #param totalSize total size of available records
* #param values the key value pairs
*/
public SendKeyValuePairsEPO(#JsonProperty("startIndex") final Integer startIndex,
#JsonProperty("size") final Integer size,
#JsonProperty("totalSize") final Integer totalSize,
#JsonProperty("values") final List<KeyValuePairEPO> values) {
this.startIndex = startIndex;
this.size = size;
this.totalSize = totalSize;
this.values = values;
}
and aswell a KeyValuePairEPO:
KeyValuePairEPO implements Serializable{
private static final long serialVersionUID = 5311348008314829094L;
private final String key;
private final String value;
private final String type; //maybe you need a type to tell what kind of value it is
...
And at last you will need to do something like:
/*package*/ <T> T sendRequest(Class<T> responseClass, Object requestEpo, String uri) {
try {
//Parse encapsulated COntent type to media type
HttpHeaders headers = new HttpHeaders();
MediaType requestContentType requestContentType = MediaType.APPLICATION_JSON;
//Set content type and accept header to this type
headers.setContentType(requestContentType);
headers.setAccept(Collections.singletonList(requestContentType));
//Parse the data object to a JSON
String requestJSONAsString = "";
if (request.getData() != null) {
try {
requestJSONAsString = RestObjectMapper.getInstance().writeValueAsString(requestEpo);
} catch (JsonProcessingException ex) {
throw new InternalServerErrorException(String.format("Error parsing: %s", requestEpo.getClass().getSimpleName()), ex);
}
}
//Perform the send request
return sendRequest(responseClass, uri, headers, httpMethod, requestJSONAsString);
} finally {
LOG.debug("Ended sendRequest");
}
}
private <T> T sendRequest(final Class<T> responseClass, final String uri, final HttpHeaders httpHeaders, final HttpMethod httpMethod, String requestJSON) {
try {
LOG.debug(String.format("Start sendRequest with:%s %s %s %s", uri, httpHeaders, httpMethod, requestJSON));
RestTemplate rest = new RestTemplate();
ClientHttpRequestFactory restFactory = rest.getRequestFactory();
if(restFactory instanceof SimpleClientHttpRequestFactory){
((SimpleClientHttpRequestFactory)restFactory).setReadTimeout(REQUEST_TIMEOUT);
((SimpleClientHttpRequestFactory)restFactory).setConnectTimeout(REQUEST_TIMEOUT);
}
HttpEntity<String> entity = new HttpEntity<>(requestJSON, httpHeaders);
final ResponseEntity<String> response = rest.exchange(uri, httpMethod, entity, String.class);
LOG.debug("Status:" + response.getStatusCode().toString());
String returnedPayload = response.getBody();
return RestObjectMapper.getInstance().readValue(returnedPayload, responseClass);
} catch (HttpStatusCodeException ex) {
LOG.error("HTTP Error in sendRequest: " + ex.getMessage());
switch (ex.getStatusCode()) {
case BAD_REQUEST:
throw new BadRequestException(uri, ex);
case NOT_FOUND:
throw new NotFoundException(uri, ex);
case FORBIDDEN:
throw new ForbiddenException(uri, ex);
case REQUEST_TIMEOUT:
throw new RequestTimeoutException(ex, REQUEST_TIMEOUT);
default:
throw new InternalServerErrorException(ex);
}
} catch (Exception ex) {
LOG.error("Error in sendRequest: " + ex.getMessage());
throw new InternalServerErrorException(ex);
} finally {
LOG.debug("Ended sendRequest");
}
}
where RestObjectMapper is:
public class RestObjectMapper extends ObjectMapper {
public static final String EMPTY_JSON = "{}";
private static final long serialVersionUID = 3924442982193452932L;
/**
* Singleton Instance
* Pattern: Initialization-on-demand holder idiom:
* <ul>
* <li>the class loader loads classes when they are first accessed (in this case Holder's only access is within the getInstance() method)</li>
* <li>when a class is loaded, and before anyone can use it, all static initializers are guaranteed to be executed (that's when Holder's static block fires)</li>
* <li>the class loader has its own synchronization built right in that make the above two points guaranteed to be threadsafe</li></ul>
*/
private static class INSTANCE_HOLDER {
private static final RestObjectMapper INSTANCE = new RestObjectMapper();
}
private RestObjectMapper() {
super();
configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
}
/**
* Gets the singleton Instance of the JSON Mapper
*
* #return the singleton instance
*/
public static RestObjectMapper getInstance() {
return INSTANCE_HOLDER.INSTANCE;
}
By the way ResponseClass is another EPO the result (JSON) will be mapped to.

What is proper practice for multithreading and httpclient in Java?

I'm creating a program that can do multiple logins. I will also give each login the ability to add an item to cart and purchase. The code is currently working for one account, and it's very basic. I had to trim out some private information, but the code should still be clear. Again, I'm just wondering what approach I should take for multiple logins? Does this code for the most part look optimal for speed? How do I approach a retry attempt if checkout returns 500? This code is currently setup for only a single login. Also, there weren't many articles I found to properly clean up the HttpClient. At least, I don't think the tutorials I found were very reputable.
Thanks again for taking the time to read this, I just want to learn other practices to improve my code and approach a proper multithreading technique.
Alittle more detail about my code, there is a token that is retrieved when you view the page, the token is stored and used throughout the program.
class example {
private static List<Header> headers = new ArrayList<Header>();
private static BasicCookieStore cookieStore = new BasicCookieStore();
private static CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultHeaders(headers).setDefaultCookieStore(cookieStore).build();
public static void main(String[] args) throws Exception {
String loginURL = "...";
String productUrl = "...";
String userid = "";
String password = "";
String formKey = "";
int size = 9;
Boolean debug = true;
JsonElement product;
headers.add(new BasicHeader("Host", "..."));
headers.add(new BasicHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"));
headers.add(new BasicHeader("Accept-Language", "en-us"));
headers.add(new BasicHeader("Content-Encoding", "gzip, deflate"));
headers.add(new BasicHeader("Content-Type", "Application/x-www-form-urlencoded"));
headers.add(new BasicHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 8_4_1 like Mac OS X) "
+ "AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H321 Safari/600.1.4"));
headers.add(new BasicHeader("Connection", "keep-alive"));
Scanner in = new Scanner(System.in);
Gson gson = new Gson();
System.out.println("Select Profile\n1. ...\n2. Custom");
int select = in.nextInt();
switch(select) {
case 1:
userid = "...";
password = "...";
break;
// Custom Account Login
case 2:
System.out.println("Enter User_ID:");
userid = in.next();
System.out.println("Enter Password:");
password = in.next();
break;
}
int input = 0;
do {
input = in.nextInt();
switch(input) {
case 99:
debug = true;
break;
// View Menu.
case 0:
System.out.println("...");
break;
// Initiate Session
case 1:
// Retrieve formKey for login page.
formKey = getLogin(GetPageContent(loginURL));
session(formKey, userid, password, debug);
System.out.println("Press 0 to View Menu");
break;
case 2:
product = getProduct(GetPageContent(productUrl));
// Retrieve Product ID
Product productInfo = gson.fromJson(product.getAsJsonObject(), Product.class);
// Retrieve Color
Product[] color = gson.fromJson(product.getAsJsonObject().getAsJsonObject("attributes").getAsJsonObject("92").getAsJsonArray("options"), Product[].class);
// Prepare product request
String productKey = productInfo.getProductId();
String colorId = color[0].getId();
String postUrl = productInfo.getPostUrl();
// Execute addToCart
String result = addToCart(formKey, postUrl, productKey, colorId, size, debug);
break;
case 3:
String result2 = checkout(formKey);
break;
}
} while(input != 0);
}
public static void session(String formKey, String userid, String password, Boolean debug) {
HttpPost post = new HttpPost("...");
try {
// Package the data
StringEntity entity = new StringEntity("...");
post.setEntity(entity);
// Execute the data
HttpResponse response = httpClient.execute(post);
} catch (Exception e) {
e.printStackTrace();
} finally {
post.releaseConnection();
System.out.println("Login execution completed.");
}
}
public static String addToCart(String formKey, String postUrl, String productId, String colorId, int size, Boolean debug) {
String result = "";
HttpPost post = new HttpPost(postUrl);
try {
StringEntity entity = new StringEntity("...");
post.setEntity(entity);
// Execute the data
HttpResponse response = httpClient.execute(post);
System.out.println("Response Code : "
+ response.getStatusLine().getStatusCode());
// RETURN RESULT
result = EntityUtils.toString(response.getEntity());
if(debug) {
System.out.println("LOG: " + result);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
post.releaseConnection();
System.out.println("Adding to cart execution completed.");
}
return result;
}
public static String GetPageContent(String url) throws Exception {
StringBuffer result = null;
HttpGet request = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(request);
int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
request.releaseConnection();
}
return result.toString();
}
public static String getLogin(String html) {
String formKey = "";
Document doc = Jsoup.parse(html);
Element loginform = doc.getElementById("login-form");
Elements inputElements = loginform.getElementsByTag("input");
for (Element inputElement : inputElements) {
String key = inputElement.attr("name");
String value = inputElement.attr("value");
if (key.equals("form_key"))
formKey = value;
}
return formKey;
}
public static JsonObject getProduct(String html)
throws UnsupportedEncodingException {
Document doc = Jsoup.parse(html);
System.out.println("Extracting form's data...");
Element form = doc.getElementById("product_addtocart_form");
Elements formElements = form.getElementsByTag("input");
String rawScript = form.getElementsByTag("script").html();
String script = "{" + rawScript.substring(rawScript.lastIndexOf("g({") + 3, rawScript.indexOf("}});")) + "}}";
// Create JSON object
JsonElement jelement = new JsonParser().parse(script.trim());
JsonObject jobject = jelement.getAsJsonObject();
jobject = jobject.getAsJsonObject();
// Retrieve post link.
jobject.addProperty("postUrl", form.attr("action"));
return jobject;
}
public static String checkout(String formKey) {
HttpPost post = new HttpPost("...");
String result = "";
try {
StringEntity entity = new StringEntity("...");
post.setEntity(entity);
HttpResponse response = httpClient.execute(post);
int status = response.getStatusLine().getStatusCode();
if(status == 200) {
result = EntityUtils.toString(response.getEntity());
System.out.println("Checkout Result: " + result);
} else if(status == 302) {
System.out.println("Checkout failed, Code: 302.");
} else if(status == 404) {
System.out.println("Checkout failed, Code: 404.");
} else if(status == 500) {
(insert retry step here)
System.out.println("Webserver is probably down. Code, 500.");
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
post.releaseConnection();
}
return result;
}
}

commons httpclient - Adding query string parameters to GET/POST request

I am using commons HttpClient to make an http call to a Spring servlet. I need to add a few parameters in the query string. So I do the following:
HttpRequestBase request = new HttpGet(url);
HttpParams params = new BasicHttpParams();
params.setParameter("key1", "value1");
params.setParameter("key2", "value2");
params.setParameter("key3", "value3");
request.setParams(params);
HttpClient httpClient = new DefaultHttpClient();
httpClient.execute(request);
However when i try to read the parameter in the servlet using
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getParameter("key");
it returns null. In fact the parameterMap is completely empty. When I manually append the parameters to the url before creating the HttpGet request, the parameters are available in the servlet. Same when I hit the servlet from the browser using the URL with queryString appended.
What's the error here? In httpclient 3.x, GetMethod had a setQueryString() method to append the querystring. What's the equivalent in 4.x?
Here is how you would add query string parameters using HttpClient 4.2 and later:
URIBuilder builder = new URIBuilder("http://example.com/");
builder.setParameter("parts", "all").setParameter("action", "finish");
HttpPost post = new HttpPost(builder.build());
The resulting URI would look like:
http://example.com/?parts=all&action=finish
If you want to add a query parameter after you have created the request, try casting the HttpRequest to a HttpBaseRequest. Then you can change the URI of the casted request:
HttpGet someHttpGet = new HttpGet("http://google.de");
URI uri = new URIBuilder(someHttpGet.getURI()).addParameter("q",
"That was easy!").build();
((HttpRequestBase) someHttpGet).setURI(uri);
The HttpParams interface isn't there for specifying query string parameters, it's for specifying runtime behaviour of the HttpClient object.
If you want to pass query string parameters, you need to assemble them on the URL yourself, e.g.
new HttpGet(url + "key1=" + value1 + ...);
Remember to encode the values first (using URLEncoder).
I am using httpclient 4.4.
For solr query I used the following way and it worked.
NameValuePair nv2 = new BasicNameValuePair("fq","(active:true) AND (category:Fruit OR category1:Vegetable)");
nvPairList.add(nv2);
NameValuePair nv3 = new BasicNameValuePair("wt","json");
nvPairList.add(nv3);
NameValuePair nv4 = new BasicNameValuePair("start","0");
nvPairList.add(nv4);
NameValuePair nv5 = new BasicNameValuePair("rows","10");
nvPairList.add(nv5);
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
URI uri = new URIBuilder(request.getURI()).addParameters(nvPairList).build();
request.setURI(uri);
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() != 200) {
}
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
String output;
System.out.println("Output .... ");
String respStr = "";
while ((output = br.readLine()) != null) {
respStr = respStr + output;
System.out.println(output);
}
This approach is ok but will not work for when you get params dynamically , sometimes 1, 2, 3 or more, just like a SOLR search query (for example)
Here is a more flexible solution. Crude but can be refined.
public static void main(String[] args) {
String host = "localhost";
String port = "9093";
String param = "/10-2014.01?description=cars&verbose=true&hl=true&hl.simple.pre=<b>&hl.simple.post=</b>";
String[] wholeString = param.split("\\?");
String theQueryString = wholeString.length > 1 ? wholeString[1] : "";
String SolrUrl = "http://" + host + ":" + port + "/mypublish-services/carclassifications/" + "loc";
GetMethod method = new GetMethod(SolrUrl );
if (theQueryString.equalsIgnoreCase("")) {
method.setQueryString(new NameValuePair[]{
});
} else {
String[] paramKeyValuesArray = theQueryString.split("&");
List<String> list = Arrays.asList(paramKeyValuesArray);
List<NameValuePair> nvPairList = new ArrayList<NameValuePair>();
for (String s : list) {
String[] nvPair = s.split("=");
String theKey = nvPair[0];
String theValue = nvPair[1];
NameValuePair nameValuePair = new NameValuePair(theKey, theValue);
nvPairList.add(nameValuePair);
}
NameValuePair[] nvPairArray = new NameValuePair[nvPairList.size()];
nvPairList.toArray(nvPairArray);
method.setQueryString(nvPairArray); // Encoding is taken care of here by setQueryString
}
}
This is how I implemented my URL builder.
I have created one Service class to provide the params for the URL
public interface ParamsProvider {
String queryProvider(List<BasicNameValuePair> params);
String bodyProvider(List<BasicNameValuePair> params);
}
The Implementation of methods are below
#Component
public class ParamsProviderImp implements ParamsProvider {
#Override
public String queryProvider(List<BasicNameValuePair> params) {
StringBuilder query = new StringBuilder();
AtomicBoolean first = new AtomicBoolean(true);
params.forEach(basicNameValuePair -> {
if (first.get()) {
query.append("?");
query.append(basicNameValuePair.toString());
first.set(false);
} else {
query.append("&");
query.append(basicNameValuePair.toString());
}
});
return query.toString();
}
#Override
public String bodyProvider(List<BasicNameValuePair> params) {
StringBuilder body = new StringBuilder();
AtomicBoolean first = new AtomicBoolean(true);
params.forEach(basicNameValuePair -> {
if (first.get()) {
body.append(basicNameValuePair.toString());
first.set(false);
} else {
body.append("&");
body.append(basicNameValuePair.toString());
}
});
return body.toString();
}
}
When we need the query params for our URL, I simply call the service and build it.
Example for that is below.
Class Mock{
#Autowired
ParamsProvider paramsProvider;
String url ="http://www.google.lk";
// For the query params price,type
List<BasicNameValuePair> queryParameters = new ArrayList<>();
queryParameters.add(new BasicNameValuePair("price", 100));
queryParameters.add(new BasicNameValuePair("type", "L"));
url = url+paramsProvider.queryProvider(queryParameters);
// You can use it in similar way to send the body params using the bodyProvider
}
Im using Java 8 and apache httpclient 4.5.13
HashMap<String, String> customParams = new HashMap<>();
customParams.put("param1", "ABC");
customParams.put("param2", "123");
URIBuilder uriBuilder = new URIBuilder(baseURL);
for (String paramKey : customParams.keySet()) {
uriBuilder.addParameter(paramKey, customParams.get(paramKey));
}
System.out.println(uriBuilder.build().toASCIIString()); // ENCODED URL
System.out.println(uriBuilder.build().toString); // NORMAL URL
Full example with DTO
public class HttpResponseDTO {
private Integer statusCode;
private String body;
private String errorMessage;
public Integer getStatusCode() {
return statusCode;
}
public void setStatusCode(Integer statusCode) {
this.statusCode = statusCode;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}
/**
*
* #param destinationURL
* #param params
* #param headers
* #return HttpResponseDTO
*/
public static HttpResponseDTO get(String baseURL, Boolean encodeURL, HashMap<String, String> params, HashMap<String, String> headers) {
final HttpResponseDTO httpResponseDTO = new HttpResponseDTO();
// ADD PARAMS IF
if (params != null && Boolean.FALSE.equals(params.isEmpty())) {
URIBuilder uriBuilder;
try {
uriBuilder = new URIBuilder(baseURL);
for (String paramKey : params.keySet()) {
uriBuilder.addParameter(paramKey, params.get(paramKey));
}
// CODIFICAR URL ?
if (Boolean.TRUE.equals(encodeURL)) {
baseURL = uriBuilder.build().toASCIIString();
} else {
baseURL = uriBuilder.build().toString();
}
} catch (URISyntaxException e) {
httpResponseDTO.setStatusCode(500);
httpResponseDTO.setErrorMessage("ERROR AL CODIFICAR URL: " + e.getMessage());
return httpResponseDTO;
}
}
// HACER PETICION HTTP
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
final HttpGet get = new HttpGet(baseURL);
// ADD HEADERS
if (headers != null && Boolean.FALSE.equals(headers.isEmpty())) {
for (String headerKey : headers.keySet()) {
get.setHeader(headerKey, headers.get(headerKey));
}
}
try (CloseableHttpResponse response = httpClient.execute(get);) {
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
httpResponseDTO.setBody(EntityUtils.toString(httpEntity));
httpResponseDTO.setStatusCode(response.getStatusLine().getStatusCode());
}
} catch(Exception e) {
httpResponseDTO.setStatusCode(500);
httpResponseDTO.setErrorMessage(e.getMessage());
return httpResponseDTO;
}
} catch(Exception e) {
httpResponseDTO.setStatusCode(500);
httpResponseDTO.setErrorMessage(e.getMessage());
return httpResponseDTO;
}
return httpResponseDTO;
}

Categories