Handle Sharp In Controller And Get Id - spring-boot

There was a jsp application. I have just converted to spring boot application. I want to continue to use same links to handle company's information. Old urls are like /Dashboard.jsp#/company/10712. I have tried to handle company id but it didn't wook. How can I handle company id ?
#GetMapping("/Dashboard.jsp#/company/{id}")
public void try(#PathVariable String id) {
System.out.println(id);
}
I have also tried;
adding
server.tomcat.relaxed-path-chars=#
in application properties.
#RequestMapping(value = ERROR_PATH, produces = "text/html")
public Object errorHtml(HttpServletRequest request, HttpServletResponse response) {
if (response.getStatus() == HttpStatus.NOT_FOUND.value()) {
return new ModelAndView("redirect:" + StringUtils.getBaseUrl(request) + "/?page=error", HttpStatus.FOUND);
} else {
return new ModelAndView("redirect:" + StringUtils.getBaseUrl(request) + "/?page=error");
}
}
This function handle 404.
request.getAttribute("javax.servlet.forward.request_uri")
returns /esir/Dashboard.jsp. There is no # and others.

Related

How to open a new web browser window in the controller in 302 code

Now my code shows 302 code as follows and there is no value in the body, so the web page does not change the page according to the return value. However, I want to automatically open a new page in the web browser by redirecting a different url depending on the return value.
What I use is mysql, thymeleaf, jpa, springboot, spring data jpa.
enter image description here
This is My #Controller code (Not #RestController)
#RequestMapping(method = RequestMethod.POST, value = "/find_email5")
public String findEmail5(FindEmailDto findEmailDto) {
String s = "failed";
try {
Optional<Users> users = userService.findEmail(findEmailDto);
if (users.isPresent()) {
String email = users.get().getEmail();
return ("redirect:/alert" + email);
}
} catch (Exception e) {
return ("/alert?value=" + s);
}
return ("/alert?value=" + s);
}
#GetMapping("/alert")
public String alertPage(#RequestParam("value") String s, Model model) {
model.addAttribute("string", s);
return "alert";
}

unable to return a page from Restcontroller

I am using stripe as a payment gateway. I just need to return a page from Webhook controller which is a rest controller. I know Restcontroller should not return a view but can't see any other option except this. Now I am using ModelandView interface to return a view but unable to do that. So please tell me that how can I return a view from restcontroller and what is wrong in this code.
#RestController
public class StripeWebhookController {
#Autowired
private FoodhubServiceImpl service;
#Autowired
private Payment payment;
private String endpointSecret="some endpointSecret";
#PostMapping("/foodhub/endpoint")
public ModelAndView handleStripeEvents(#RequestBody String payload, #RequestHeader("Stripe-Signature") String sigHeader, HttpServletRequest request) {
if(sigHeader == null) {
System.out.println("sigheader is null");
return null;
}
Event event;
// Only verify the event if you have an endpoint secret defined.
// Otherwise use the basic event deserialized with GSON.
try {
event = Webhook.constructEvent(
payload, sigHeader, endpointSecret
);
} catch (SignatureVerificationException e) {
// Invalid signature
System.out.println("Webhook error while validating signature.");
System.out.println(e);
return null;
}
// Deserialize the nested object inside the event
EventDataObjectDeserializer dataObjectDeserializer = event.getDataObjectDeserializer();
StripeObject stripeObject = null;
if (dataObjectDeserializer.getObject().isPresent()) {
stripeObject = dataObjectDeserializer.getObject().get();
} else {
// Deserialization failed, probably due to an API version mismatch.
// Refer to the Javadoc documentation on `EventDataObjectDeserializer` for
// instructions on how to handle this case, or return an error here.
}
// Handle the event
switch (event.getType()) {
case "payment_intent.succeeded":
PaymentIntent paymentIntent = (PaymentIntent) stripeObject;
System.out.println("Payment succeeded for "+paymentIntent.getAmount());
handlePaymentIntentSucceeded(paymentIntent);
// Then define and call a method to handle the successful payment intent.
break;
default:
System.out.println("Unhandled event type: " + event.getType());
break;
}
ModelAndView mv = new ModelAndView();
mv.setViewName("payment-success.jsp");
return mv;
}

what is the standard way to design a high throughput spring based web serivce with Async in mind

I have a requirement to design spring-boot based webservice with #Async tags.
if the data return from server takes huge time how can i divide my webservice in multiple end points so that client does not have to wait for response from server.
i hv tried using three end points.
1. localhost:8080/start -> client will send request for data. (returs a uuid of requestnumber).
2. localhost:8080/checkprogress -> check for progress if data is ready at server.
3. localhost:8080/done/requestId -> return the data list
#ResponseBody
#GetMapping(value = "/start")
public ResponseEntity<String> start() {
String requestId = UUID.randomUUID().toString();
LOG.info("jobName" + requestId);
Job job = new Job("jobName" + requestId);
requestQueue.put(requestId.toString(), job);
service.submitWork(job);
return new ResponseEntity<String>(requestId, HttpStatus.ACCEPTED);
}
#ResponseBody
#RequestMapping(value = "/progress/{requestId}")
public ResponseEntity<String> fetchStatus(#PathVariable("requestId") String requestId) {
Job job = requestQueue.get(requestId);
if (job == null) {
return new ResponseEntity<String>("RequestId is either invalid or already served. requestId:" + requestId,
HttpStatus.BAD_REQUEST);
}
if (job.getState() == State.RUNNING || job.getState() == State.NEW)
return new ResponseEntity<String>(HttpStatus.NO_CONTENT);
if (job.getState() == State.DONE)
return new ResponseEntity<String>(HttpStatus.OK);
return null;
}
#ResponseBody
#RequestMapping(value = "/done/{requestId}")
public ResponseEntity<Object> done(#PathVariable("requestId") String requestId) {
LOG.info("removing requestId:" + requestId);
Job job = requestQueue.get(requestId);
if (job == null) {
return new ResponseEntity<Object>("RequestId is either invalid or already served. requestId:" + requestId,
HttpStatus.BAD_REQUEST);
}
// ResponseEntity<Object> response = new ResponseEntity<Object>(job.getList(),
// createHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE), HttpStatus.OK);
ResponseEntity<Object> response = new ResponseEntity<Object>(job.getListOfSecurities(),
createHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE), HttpStatus.OK);
requestQueue.remove(requestId);
return response;
}
is there a better way of doing above thing in a Standard way in spring-boot java8 application please?

how to access GetMapping notation from postman with HttpServletRequest

I have a spring boot controller but I don't know how to access the GetMapping notation through postman application. This is my controller:
#GetMapping
public ResponseEntity<dataTableDTO> getProject(HttpServletRequest request, int draw) throws Exception {
//... do what needs to be done
List<ProjectEntity> objProj = (List<ProjectEntity>) projectRepository.findAll();
List<String> slist = new ArrayList<String>();
for(ProjectEntity d : (List<ProjectEntity>)objProj){
slist.add(String.valueOf(d.getCustomerId()));
}
String listCustId = StringUtils.collectionToCommaDelimitedString(slist);
List<CustomerDTO> objCust = (new CustomerDAO()).getCustomer(listCustId, request.getHeader("Authorization"));
List<ProjectDTO> objProjDTO = new ArrayList<ProjectDTO>();
for(ProjectEntity d : (List<ProjectEntity>)objProj){
String name = "";
for(CustomerDTO c : objCust){
if(c.getId() == d.getCustomerId()){
name = c.getFirstName() + " " + c.getLastName();
}
}
objProjDTO.add(new ProjectDTO(d.getId(), d.getCustomerId(), name, d.getName(), d.getType()));
}
dataTableDTO data = new dataTableDTO(draw, objProjDTO.size(), objProjDTO.size(), objProjDTO, null);
return new ResponseEntity<dataTableDTO>(data, HttpStatus.OK);
}
I just want to know how to access the GetMapping notation through postman. I already try but i got error
error image
Put a #RequestParam annotation on your draw variable?
#GetMapping
public ResponseEntity<dataTableDTO> getProject(HttpServletRequest request, #RequestParam(name="draw") int draw) throws Exception {...}

HttpURLConnnection request failures on Android 6.0 (MarshMallow)

I am using Google's Volley library for my application project , that targets minimum api level 14.So, Volley library uses HttpURLConnection as the NetworkClient.
Therefore , there should not be any issue related to Removal of Apache HTTPClient. However, I have done the configuration required for 6.0 Sdk i.e compileSdkVersion 23, build-tool-version 23.0.1 and build:gradle:1.3.1' and even tried adding useLibrary 'org.apache.http.legacy'. Have updated the same for Volley library project in my application.
Recently ,I tried to run my app on Android 6.0 (MarshMallow), my project compiles and runs. But those requests that require authentication headers are failing on MarshMallow with:
BasicNetwork.performRequest: Unexpected response code 401 com.android.volley.AuthFailureError
However the same is running on all Api level below 23.
I have checked the headers many times.Strangely, those requests that do not require authentication are giving response with 200 OK.
Right now I am totally clueless what is breaking the requests, does anybody have any idea what has changed in new Version that HttpURLConnection request fails for only Api level 23? Is anybody else using Volley and facing similar issue?
Here is my CustomRequest Class
public class CustomRequest extends Request<Void> {
int id, cmd;
Map<String, String> params;
BaseModel model;
public CustomRequest(int method, int cmd, String url, Map<String, String> params, int id, BaseModel model) {
super(method, url, null);
this.id = id;
this.cmd = cmd;
this.params = params;
this.model = model;
if (method == Method.GET) {
setUrl(buildUrlForGetRequest(url));
}
Log.v("Volley", "Making request to: " + getUrl());
}
private String buildUrlForGetRequest(String url) {
if (params == null || params.size() == 0) return url;
StringBuilder newUrl = new StringBuilder(url);
Set<Entry<String, String>> paramPairs = params.entrySet();
Iterator<Entry<String, String>> iter = paramPairs.iterator();
newUrl.append("?");
while (iter.hasNext()) {
Entry<String, String> param = iter.next();
newUrl
.append(param.getKey())
.append("=")
.append(param.getValue());
if (iter.hasNext()) newUrl.append("&");
}
return newUrl.toString();
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("X-Api-Version", Contract.API_VERSION);
headers.put("X-Client", "android");
String accessToken = APP.getInstance().getToken();
if (!accessToken.equals("")) {
headers.put("Authorization", "Bearer " + accessToken);
}
return headers;
}
#Override
protected Response<Void> parseNetworkResponse(NetworkResponse response) {
Exception ex;
try {
String jsonString = new String(
response.data, HttpHeaderParser.parseCharset(response.headers));
JsonNode json = new ObjectMapper().readTree(jsonString);
if (model != null) model.parse(id, json);
EventBus.getDefault().post(new EventResponse(cmd, true, null));
return Response.success(null, HttpHeaderParser.parseCacheHeaders(response)); //Doesn't return anything. BaseModel.parse() does all the storage work.
} catch (NoMoreDataException e) {
ex = e;
EventBus.getDefault().post(new NoMoreDataModel(cmd, e));
EventBus.getDefault().post(new EventResponse(cmd, false, null));
return Response.success(null, HttpHeaderParser.parseCacheHeaders(response));
} catch (Exception e) {
ex = e;
Log.e("CustomRequest", Log.getStackTraceString(e));
String message = APP.getInstance().getString(R.string.failedRequest);
if (!TextUtils.isEmpty(e.getMessage()))
message = e.getMessage();
EventBus.getDefault().post(new ErrorEventModel(cmd, message, e));
return Response.error(new ParseError(ex));
}
}
#Override
protected Map<String, String> getParams() throws AuthFailureError {
return params;
}
#Override
protected void deliverResponse(Void response) {
Log.v("Volley", "Delivering result: " + getUrl());
}
#Override
public void deliverError(VolleyError error) {
Log.e("CustomRequest", "Delivering error: Request=" + getUrl() + " | Error=" + error.toString());
String message = APP.getInstance().getString(R.string.failedRequest);
EventBus.getDefault().post(new ErrorEventModel(cmd, message, error));
}
}
Only difference I found between Api 23 and others is the HostNameVerifier.
For Api level 23 : com.android.okhttp.internal.tls.OkHostnameVerifier
For Api level <23 : javax.net.ssl.DefaultHostnameVerifier.
After checking the Server side of my application, found the reason behind the issue.
For Android 6.0(MarshMallow) the headers were becoming empty and this was not the case with other versions.
So the fix that worked for me:
Created and Added a new custom header X-Proxy-No-Redirect => 1 and passed along with other headers.
Theory behind it:
There is a API server to which we send request, then the API server redirects the request to corresponding site based on the oAuth token
While redirecting
For the redirection to happen, there are two ways to do that
1 - We just send a response back to the caller stating to redirect to a certain page. Then the caller(Networking library) takes care of redirection
2 - API server will itself makes the redirect request and get the response and then pass to caller
Earlier we were using the Method 1.
On Android 6.0 - The networking lib(Volley) doesn't seem to set all the headers while making the redirection request.
Once this new header is set, Method 2 will come into effective.
P.S This fix was applicable for my application project , maybe not for others.Just providing what was wrong and what helped me.

Resources