Skip to content

Instantly share code, notes, and snippets.

@jeorfevre
Forked from SrikanthRao/DateResource.java
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeorfevre/6e46ae8d9232f7f9d7cc to your computer and use it in GitHub Desktop.
Save jeorfevre/6e46ae8d9232f7f9d7cc to your computer and use it in GitHub Desktop.
package com.rizze.stackoverflow;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Produces;
import javax.ws.rs.ServerErrorException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import com.rizze.web.ResultException;
@Provider
public class _ServerError implements ExceptionMapper<ServerErrorException>{
@Context HttpServletRequest request;
@Override
@Produces({MediaType.APPLICATION_JSON,MediaType.TEXT_PLAIN})
public Response toResponse(ServerErrorException ex){
ResultException tec= new ResultException();
tec.status(500);
tec.appendComment("{server} error");
tec.setException(ex,request);
return tec.getResponse();
}
}
package com.rizze.stackoverflow;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
@Path("/test")
public class TestRessource {
@GET
@Path("is")
@Produces({"application/json"})
public Response isDateValid(@QueryParam("date") String dateString){
if(dateString == null)
return Response.status(400).entity("date is null").build();
Date thedate =null;
try {
thedate = new SimpleDateFormat("yyyy-mm-dd").parse(dateString);
}
catch (ParseException e) {
return Response.status(400).entity("date malformed").build();
}
System.out.println("time:"+thedate.getTime());
return Response.ok()
.entity("{\"time\":"+ thedate.getTime() +"}")
.header("source", "com.rizze")
.build();
}
}
package com.fun.myapp;
import io.dropwizard.jersey.params.AbstractParam;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
/**
* Converts ISO_LOCAL_DATE to {@code LocalDateTime}
*
*/
public class LocalDateTimeParam extends AbstractParam<LocalDateTime> {
public LocalDateTimeParam(String input) {
super(input);
}
@Override
protected LocalDateTime parse(String input) throws Exception {
return LocalDateTime.of(LocalDate.parse(input), LocalTime.MIDNIGHT);
}
@Override
protected Response error(String input, Exception e) {
return Response
.status(Status.BAD_REQUEST)
.type(MediaType.APPLICATION_JSON_TYPE)
.entity("Date is either not in YYYY-MM-DD format or is invalid")
.build();
}
}
package com.fun.myapp;
import org.glassfish.hk2.api.MultiException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
public class MultiExceptionMapper implements ExceptionMapper<MultiException> {
@Override
public Response toResponse(MultiException exception) {
for (Throwable ex : exception.getErrors()) {
if (WebApplicationException.class.isAssignableFrom(ex.getClass())) {
return ((WebApplicationException) ex).getResponse();
}
}
return Response
.serverError()
.entity("Internal Error occurred.")
.type(MediaType.APPLICATION_JSON_TYPE)
.build();
}
}
package com.fun.myapp;
import io.dropwizard.Application;
import io.dropwizard.setup.Environment;
public class MyApp extends Application<MyAppConfig> {
@Override
public void run(MyAppConfig configuration, Environment environment) throws Exception {
environment.jersey().register(DateResource.class);
//register a mapper by rizze
environment.jersey().register(_ServerError.class);
}
public static void main(String[] args) throws Exception {
new MyApp().run(args);
}
}
package com.fun.myapp;
import io.dropwizard.Configuration;
public class MyAppConfig extends Configuration {
public MyAppConfig() {
register(_ServerError);
}
}
package com.fun.myapp;
import java.time.LocalDateTime;
import java.util.Optional;
import javax.validation.constraints.NotNull;
import javax.ws.rs.QueryParam;
public class PaginationFilters {
public PaginationFilters() {
// empty constructor
}
@NotNull(message = "date must be specified")
@QueryParam("date")
private LocalDateTimeParam date;
public Optional<LocalDateTime> getDate() {
if (date != null) {
return Optional.of(date.get());
}
else {
return Optional.empty();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment