-
-
Save jeorfevre/6e46ae8d9232f7f9d7cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fun.myapp; | |
import io.dropwizard.Configuration; | |
public class MyAppConfig extends Configuration { | |
public MyAppConfig() { | |
register(_ServerError); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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