I have RESTeasy service. And have implemented simple error handling on methods using try catch and feel something is not very well with it. I've noticed try catch repetition on all my methods. So I want ask way how to avoid repetition (to reduce code size) of try catch but not lost functionality.
#Path("/rest")
#Logged
#Produces("application/json")
public class CounterRestService {
#POST
#Path("/create")
public CounterResponce create(#QueryParam("name") String name) {
try {
CounterService.getInstance().put(name);
return new CounterResponce();
} catch (Exception e){
return new CounterResponce("error", e.getMessage());
}
}
#POST
#Path("/insert")
public CounterResponce create(Counter counter) {
try {
CounterService.getInstance().put(counter);
return new CounterResponce();
} catch (Exception e){
return new CounterResponce("error", e.getMessage());
}
}
#DELETE
#Path("/delete")
public CounterResponce delete(#QueryParam("name") String name) {
try {
CounterService.getInstance().remove(name);
return new CounterResponce();
} catch (Exception e){
return new CounterResponce("error", e.getMessage());
}
}
... // other methods with some try catch pattern
response
public class CounterResponce {
private String status;
#JsonSerialize(include=Inclusion.NON_NULL)
private Object data;
public CounterResponce() {
this.status = "ok";
}
public CounterResponce(Object o) {
this.status = "ok";
this.data = o;
}
public CounterResponce(String status, Object o){
this.status = status;
this.data = o;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
exceptions source
public class CounterService {
private Map<String, StatisticCounter> counters = new HashMap<String, StatisticCounter>();
private static CounterService instance = null;
protected CounterService() {}
public static CounterService getInstance() {
if(instance == null) {
instance = new CounterService();
}
return instance;
}
public StatisticCounter get(String name){
StatisticCounter c = counters.get(name);
if(c == null)throw new IllegalArgumentException("Counter "+name+" not exist");
return c;
}
public void put(String name){
if(name==null)throw new IllegalArgumentException("null can`t be as name");
if(counters.get(name)!=null)throw new IllegalArgumentException("Counter "+name+" exist");
counters.put(name, new Counter(name));
}...
The comments in your question are pointing you in a good direction. Since the answers do not mention it, I'll summarize the general idea in this answer.
Extending WebApplicationException
JAX-RS allows to define direct mapping of Java exceptions to HTTP error responses. By extending WebApplicationException, you can create application specific exceptions that build a HTTP response with the status code and an optional message as the body of the response.
The following exception builds a HTTP response with the 404 status code:
public class CustomerNotFoundException extends WebApplicationException {
/**
* Create a HTTP 404 (Not Found) exception.
*/
public CustomerNotFoundException() {
super(Responses.notFound().build());
}
/**
* Create a HTTP 404 (Not Found) exception.
* #param message the String that is the entity of the 404 response.
*/
public CustomerNotFoundException(String message) {
super(Response.status(Responses.NOT_FOUND).
entity(message).type("text/plain").build());
}
}
WebApplicationException is a RuntimeException and doesn't need to the wrapped in a try-catch block or be declared in a throws clause:
#Path("customers/{customerId}")
public Customer findCustomer(#PathParam("customerId") Long customerId) {
Customer customer = customerService.find(customerId);
if (customer == null) {
throw new CustomerNotFoundException("Customer not found with ID " + customerId);
}
return customer;
}
Creating ExceptionMappers
In other cases it may not be appropriate to throw instances of WebApplicationException, or classes that extend WebApplicationException, and instead it may be preferable to map an existing exception to a response.
For such cases it is possible to use a custom exception mapping provider. The provider must implement the ExceptionMapper<E extends Throwable> interface. For example, the following maps the JAP EntityNotFoundException to a HTTP 404 response:
#Provider
public class EntityNotFoundExceptionMapper
implements ExceptionMapper<EntityNotFoundException> {
#Override
public Response toResponse(EntityNotFoundException ex) {
return Response.status(404).entity(ex.getMessage()).type("text/plain").build();
}
}
When an EntityNotFoundException is thrown, the toResponse(E) method of the EntityNotFoundExceptionMapper instance will be invoked.
The #Provider annotation declares that the class is of interest to the JAX-RS runtime. Such class may be added to the set of classes of the Application instance that is configured.
Introduce a private method such as "apply" which can take function as parameter if you use Java 8. This method will have the error handling and/or mapping, response mapping and response generation code centralized.
From create and delete methods, invoke this apply method and pass the desired counter operation you wish to perform as a lambda expression.
Related
I am using smtp api which throws MessageException and IOException
But in our application, we need to have wrapper exception for both.
Is it possible to write wrapper exception for this? like custom exception?
Sure. Exceptions just are, and can wrap anything; you don't need to write them as specifically wrapping only IOException or MessageExceptions.
public class MyCustomException extends Exception {
public MyCustomException(String msg) {
super(msg);
}
public MyCustomException(String msg, Throwable cause) {
super(msg, cause);
}
}
The above is what all custom exceptions look like (where relevant they might have a few more fields that register specific info for a specific failure, e.g. SQLException has methods to ask for the DB 'error code'), but they all at least have the above.
Then, to wrap:
public void myMethod() throws MyException {
try {
stuffThatThrowsIOEx();
stuffThatThrowsMessageEx();
} catch (MessageException | IOException e) {
throw new MyException("Cannot send foo", e);
}
}
NB: The string you pass to your MyException should be short, should not use either caps or exclamation points, or for that matter any other punctuation at the end of it. In addition, include actual relevant content there too: For example, the user you tried to send a message for (the point is, whatever you include there as a string constant needs to be simple, short, and not end in punctuation).
Consider to create a root Exception container as
public class GeneralExceptionContainer extends RuntimeException{
private Integer exceptionCode;
private String message;
public GeneralExceptionContainer(String argMessage, Integer exceptionCode) {
super(argMessage);
this.exceptionCode = exceptionCode;
this.message = argMessage;
}
public GeneralExceptionContainer(Throwable cause, Integer exceptionCode, String argMessage) {
super(argMessage, cause);
this.exceptionCode = exceptionCode;
this.message = argMessage;
}
}
With some enumeration or serialization requirement you can add exceptionCode as well
public enum ExceptionCode {
SECTION_LOCKED(-0),
MAPPING_EXCEPTION(-110)
private final int value;
public int getValue() {
return this.value;
}
ExceptionCode(int value) {
this.value = value;
}
public static ExceptionCode findByName(String name) {
for (ExceptionCode v : values()) {
if (v.name().equals(name)) {
return v;
}
}
return null;
}
}
Then extend your customException from root GeneralException Containner
public class CustomException extends GeneralExceptionContainer {
public MappingException(ExceptionCode exceptionCode) {
super(exceptionCode.name(), exceptionCode.getValue());
}
}
Is it possible to configure custom factories to generate values for the EqualsMethodTester and HashCodeMethodTester classes from org.meanbean.test? When I pass the Configuration which works for BeanTester to EqualsMethodTester, I get the following messages in the error traceback:
org.meanbean.factories.ObjectCreationException: Failed to create a value for property [demoUrl].
Failed to find suitable Factory for property=[demoUrl] of type=[class java.net.URL]. Please register a custom Factory.
org.meanbean.factories.ObjectCreationException: Failed to instantiate object of type [java.net.URL] due to NoSuchMethodException.
java.lang.NoSuchMethodException: java.net.URL.<init>()
(Both EqualsMethodTester and HashCodeMethodTester give this error. Adding "demoUrl" to the list of insignificantProperties for EqualsMethodTester().testEqualsMethod() makes no difference. Stepping through the code implies my URLFactory.create() isn't called at all.)
I do not see any options for passing the configuration into HashCodeMethodTester. I've skimmed documentation at the following sites, but have found neither a solution nor acknowledgement of the missing functionality: http://meanbean.sourceforge.net/docs/2.0.3/public/org/meanbean/test/EqualsMethodTester.html
http://meanbean.sourceforge.net/docs/2.0.3/public/org/meanbean/test/HashCodeMethodTester.html
http://meanbean.sourceforge.net/docs/2.0.3/public/org/meanbean/test/ConfigurationBuilder.html
http://meanbean.sourceforge.net/docs/2.0.3/Mean_Bean_2.0.3_User_Guide.pdf
(I'm using MeanBean v 2.0.3 and Java 1.8.)
I have the following class, using java.net.URL:
public class Product {
private String name;
private URL demoUrl;
public Product(){
super();
}
public int hashCode() {
return Objects.hash(getName(), whitehawkSKU);
}
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj == this) {
return true;
}
if (obj.getClass() != getClass()) {
return false;
}
Product other = (Product) obj;
return Objects.equals(getName(), other.getName());
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public URL getDemoUrl() {
return demoUrl;
}
public void setDemoUrl(URL demoUrl) {
this.demoUrl = demoUrl;
}
}
To handle the URL field, I created a custom factory, as per meanbean: failed to test bean with arrays and it works for BeanTester but not for EqualsMethodTester:
import org.meanbean.lang.Factory;
import java.net.MalformedURLException;
import java.net.URL;
public class URLFactory implements Factory<URL> {
private static int counter = 0;
#Override
public URL create() {
String host = "http://test." + counter + ".url/";
try {
return new URL(host);
}
catch (MalformedURLException except) {
return null;
}
}
}
My test methods are as follows:
private Configuration configureMeanBeanTests() {
URLFactory urlFactory = new URLFactory();
return new ConfigurationBuilder()
.overrideFactory("demoUrl", urlFactory).build();
}
#Test
public void testAccessors() {
new BeanTester().testBean(Product.class, configureMeanBeanTests());
}
#Test
public void testEquals() {
new EqualsMethodTester().testEqualsMethod(
Product.class,
configureMeanBeanTests(),
"name",
"demoUrl"
);
}
#Test
public void testHashCode() {
new HashCodeMethodTester().testHashCodeMethod(Product.class);
}
What am I missing?
It looks like the EqualsMethodTester().testEqualsMethod() needs a EquivalentFactory in that particular case due to the use java.net.URL that does not provide a default empty constructor. So when BasicNewObjectInstanceFactory.create() is called for java.net.URL the call the clazz.getDeclaredConstructor() throw an exception Method threw 'java.lang.NoSuchMethodException' exception..
Basically you just have to implement a EquivalentFactory.
An anonymous implementation could be:
private EquivalentFactory<Product> productEquivalentFactory = new EquivalentFactory<Product>() {
#Override
public Product create() {
Product p = new Product();
try {
p.setDemoUrl(new URL("http://test.1.url/"));
} catch (MalformedURLException e) {
e.printStackTrace();
}
p.setName("test");
return p;
}
};
It has to be used with the custom configuration that you already have:
new EqualsMethodTester().testEqualsMethod(productEquivalentFactory, configureMeanBeanTests(), "demoUrl");`
For the hashcode just use the equivalent factory and it does the job.
I tested it and it is working.
My problem lies in the fields of JsonSerialization as implemented in Jackson FasterXML Library. I have a series of endpoints through which I exchange content between my back-end and a MVVM front-end framework. This is working, but now I am a little stuck as I got to the point where I want to handle user creation/registration.
This is the model (entity) that represents a group in my application (I omit irrelevant import declarations and JPA annotations):
#JsonRootName(value="userGroup")
#JsonInclude(JsonInclude.Include.NON_NULL)
public class Groups extends MetaInfo implements Serializable {
private String groupName;
private Set<Credential> credentials = new HashSet<>();
public Groups() {
super();
}
public Groups(String groupName) {
this();
this.groupName = groupName;
}
public Groups(String createdBy, String groupName) {
this();
setCreatedBy(createdBy);
this.groupName = groupName;
}
#JsonGetter("group_Name")
// #JsonValue
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
updateModified();
}
#JsonIgnore
public Set<Credential> getCredentials() {
return credentials;
}
public void setCredentials(Set<Credential> credentials) {
this.credentials = credentials;
}
public void addCredential(Credential c) {
credentials.add(c);
if (c.getGroup() != this) {
c.setGroup(this);
}
}
#Override
public String toString() {
return "Groups{" + "groupName=" + groupName + '}';
}
}
And this is the method in the endpoint that retrieves (if exists) and returns a serialized version of a Groups to a JavaScript client:
#Path("/groups")
#Produces("application/json")
public class GroupsResourceService extends RimmaRestService{
#Inject
#Production
private GroupsRepository groupsRepository;
...
#GET
#Path("{group}")
public Response getGroup(#PathParam("group") String group){
if(InputValidators.stringNotNullNorEmpty.apply(group)){
//proceed with making call to the repo
Optional<Groups> optGroup = ofNullable(groupsRepository.getByGroupName(group));
if(optGroup.isPresent()){//ultimately success scenario
try {
String output = getMapper().writeValueAsString(optGroup.get());
return Response.ok(output).build();
} catch (JsonProcessingException e) {
logger.error("Serialization error: "+e.getMessage()+
"\n"+e.getClass().getCanonicalName());
throw new InternalServerErrorException("Server error "
+ " serializing the requested group \""+group+"\"");
}
} else{
throw new NotFoundException("Group " + group + " could not be found");
}
}else{//empty space after the slash
throw new BadRequestException("You haven't provided a group parameter",
Response.status(Response.Status.BAD_REQUEST).build());
}
}
}
Trying to test this code like this:
#Test
public void successfullResponse(){
Response success = groupsResourceService.getGroup("admin");
assertTrue(success.getStatus()==200);
}
...cruelly fails:
<< ERROR!
javax.ws.rs.InternalServerErrorException: Server error serializing the requested group "admin"
at com.vgorcinschi.rimmanew.rest.services.GroupsResourceService.getGroup(GroupsResourceService.java:54)
at com.vgorcinschi.rimmanew.services.GroupsResourceServiceTest.successfullResponse(GroupsResourceServiceTest.java:48)
In this case stack trace is of 0 help, though - that's why I am pasting the output of the log that catches the underlying Json exception:
15:05:05.857 [main] ERROR com.vgorcinschi.rimmanew.rest.services.GroupsResourceService - Serialization error: Can not write a field name, expecting a value
com.fasterxml.jackson.core.JsonGenerationException
Having visited and analyzed 13 similar complaints links (of which 3 are from stackoverflow) I came up with a solution which is more a workaround - if you look back at the entity, I have commented #JsonValue. If I uncomment that and comment #JsonGetter("group_Name") then the test passes with the following output:
{"userGroup":"admin"}
This being only a workaround, I decided to recur to asking for help which I hope someone will be able and kind enough to provide.
I'm using Wicket 7 and extending AuthenticatedWebSession as a class called BasicAuthenticatedSession. While I'm doing this, I'd like to add a method that returns some additional information about the authenticated user.
In BasicAuthenticatedSession#authenticate, I get a Sysuser object which is a wrapper for a user in the database. I use some of the information in this object for the authentication, but want to have access to all of the info (firstname, lastname, etc.) throughout the session.
I was expecting to be able to create a new method call getUser which would return this database object to the caller.
However, this method, even though declared public, isn't visible when attempting to call it. I'm not sure if this is something to do with Wicket, or just a general misunderstanding on my part how inheritance works. ;)
BasicAuthenticatedWebSession.java
public class BasicAuthenticatedWebSession extends AuthenticatedWebSession {
public BasicAuthenticatedWebSession(Request request) {
super(request);
}
#Override
protected boolean authenticate(String username, String password) {
Sysuser[] sysusers;
try {
SysuserCriteria userCriteria = new SysuserCriteria();
userCriteria.username.eq(username);
sysusers = Sysuser.listSysuserByCriteria(userCriteria);
} catch (PersistentException e) {
e.printStackTrace();
return false;
}
if (sysusers.length == 0) {
return false;
}
this.username = username;
this.userid = sysusers[0].getId();
return password.compareTo(sysusers[0].getPasswd()) == 0;
}
public Roles getRoles() {
Roles roles = new Roles();
Sysuser[] sysusers;
if (isSignedIn()) {
roles.add("SIGNED_IN");
}
try {
SysuserCriteria sysuserCriteria = new SysuserCriteria();
sysuserCriteria.username.eq(username);
sysusers = Sysuser.listSysuserByCriteria(sysuserCriteria);
for (Object sysuser : sysusers) {
SysroleSetCollection sysroles = ((Sysuser) sysuser).sysrole;
for (Sysrole sysrole : sysroles.toArray()) {
roles.add(sysrole.getRolename().toUpperCase());
}
}
} catch (PersistentException e) {
e.printStackTrace();
}
return roles;
}
public Sysuser getSysuser() {
return sysuser;
}
}
Test.java This class fails to compile as the getSysuser method in BasicAuthenticatedSession is not found.
public class Test {
public Test() {
}
public void foo {
if(BasicAuthenticatedSession.get().isSignedIn()) {
Sysuser sysUser = BasicAuthenticatedSession.get().getSysuser();
System.out.println(sysuser.getFirstname);
}
}
}
Wicket project require specific "override" of static methods, I guess that You return original wicket API session. Edited copy from my project (session is Your classname)
public class BasicAuthenticatedWebSession extends AuthenticatedWebSession {
public static BasicAuthenticatedWebSession get() {
return (BasicAuthenticatedWebSession ) Session.get();
}
...
}
and in XxxxxApplication class
public class MyProject extends AuthenticatedWebApplication
{
...
#Override
public Session newSession(Request request, Response response) {
return new BasicAuthenticatedWebSession (request);
}
}
I am trying to pull data from class in another class and populate a JPanel with the data, but it is not working for some reason.
Here is the full restConnector class where I pull the JSON data.
As far as I know this works fine.
public class restConnector {
private static final Logger LOGGER = LoggerFactory.getLogger(restConnector.class);
private static final restConnector INSTANCE = new restConnector();
public static restConnector getInstance() {
return restConnector.INSTANCE;
}
private restConnector(){
}
private static String user = "ss";
private static String pwd = "ee
public static String encode(String user, String pwd) {
final String credentials = user+":"+pwd;
BASE64Encoder encoder = new sun.misc.BASE64Encoder();
return encoder.encode(credentials.getBytes());
}
//Open REST connection
public static void init() {
restConnector.LOGGER.info("Starting REST connection...");
try {
Client client = Client.create();
client.addFilter(new LoggingFilter(System.out));
WebResource webResource = client.resource("https://somewebpage.com/
String url = "activepersonal";
ClientResponse response = webResource
.path("api/alerts/")
.queryParam("filter", ""+url)
.header("Authorization", "Basic "+encode(user, pwd))
.header("x-api-version", "1")
.accept("Application/json")
.get(ClientResponse.class);
if (response.getStatus() != 200) {
}else{
restConnector.LOGGER.info("REST connection STARTED.");
}
String output = response.getEntity(String.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(new MyNameStrategy());
try {
List<Alert> alert = mapper.readValue(output, new TypeReference<List<Alert>>(){});
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
}
}
However, when I try to pull the data in another class it gives me just null values from the system.out.print inside refreshData() method. Here is the code that is supposed to print the data
public class Application{
Alert alerts = new Alert();
public Application() {
refreshData();
}
private void initComponents() {
restConnector.init();
refreshData();
}
private void refreshData() {
System.out.println("appalertList: "+alerts.getComponentAt(0));
}
}
Here is my Alert class
#JsonIgnoreProperties(ignoreUnknown = true)
#JsonInclude(Include.NON_EMPTY)
public class Alert {
private int pasID;
private String status;
private boolean shared;
private String header;
private String desc;
public int getPasID() {
return pasID;
}
public void setPasID(int pasID) {
this.pasID = pasID;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public boolean isShared() {
return shared;
}
public void setShared(boolean shared) {
this.shared = shared;
}
public String getHeader() {
return header;
}
public void setHeader(String header) {
this.header = header;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("\n***** Alert Details *****\n");
sb.append("PasID="+getPasID()+"\n");
sb.append("Status="+getStatus()+"\n");
sb.append("Shared="+isShared()+"\n");
sb.append("Header="+getHeader()+"\n");
sb.append("Description="+getDesc()+"\n");
sb.append("*****************************");
return sb.toString();
}
public String getComponentAt(int i) {
return toString();
}
}
I'm kind a lost with this and been stuck here for a couple of days already so all help would be really appreciated. Thanks for the help in advance.
Edit: Formatted the code a bit and removed the NullPointerException as it was not happening anymore.
As stated in comments:
Me: In your first bit of code you have this try { List<Alert> alert.., but you do absolutely nothing with the newly declared alert List<Alert>. It this where the data is supposed to be coming from?
OP: I'm under the impression that that bit of code is the one that pushes the JSON Array to the Alert.class. Is there something I'm missing there?
Me: And what makes you think it does that? All it does is read the json, and the Alert.class argument is the class type argument, so the mapper know the results should be mapped to the Alert attributes when it creates the Alert objects. That's how doing List<Alert> is possible, because passing Alert.class decribes T in List<T>. The List<Alert> is what's returned from the reading, but you have to determine what to actually do with the list. And currently, you do absolutely nothing with it
You maybe want to change the class just a bit.
Now this is in no way a good design, just an example of how you can get it to work. I would take some time to sit and think about how you want the restConnector to be fully utilized
That being said, you can have a List<Alert> alerts; class member in the restConnector class. And have a getter for it
public class restConnector {
private List<Alert> alerts;
public List<Alert> getAlerts() {
return alerts;
}
...
}
Then when deserializing with the mapper, assign the value to private List<Alert> alerts. What you are doing is declaring a new locally scoped list. So instead of
try {
List<Alert> alert = mapper.readValue...
do this instead
try {
alerts = mapper.readValue
Now the class member is assigned a value. So in the Application class you can do something like
public class Application {
List<Alert> alerts;
restConnector connect;
public Application() {
initComponents();
}
private void initComponents() {
connector = restConnector.getInstance();
connector.init();
alerts = connector.getAlerts();
refreshData();
}
private void refreshData() {
StringBuilder sb = new StringBuilder();
for (Alert alert : alerts) {
sb.append(alert.toString()).append("\n");
}
System.out.println("appalertList: "+ sb.toString());
}
}
Now you have access to the Alerts in the list.
But let me reiterate: THIS IS A HORRIBLE DESIGN. For one you are limiting the init method to one single call, in which it is only able to obtain one and only one resource. What if the rest service needs to access a different resource? You have made the request set in stone, so you cant.
Take some time to think of some good OOP designs where the class can be used for different scenarios.