Expand my Community achievements bar.

SOLVED

The method adaptoTo(Class) is undefined for the type ResourceResolver error

Avatar

Level 4

I'm creating java servlet and I have problem with compilation. It says "The method adaptoTo(Class) is undefined for the type ResourceResolver". I'm following this tutorial and when I try to compile it, I get this error:

The method adaptoTo(Class) is undefined for the type ResourceResolver line: 81

This is part of my servlet code

import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; /* other imports */ public class ServletMailer extends SlingAllMethodsServlet { /* other methods */ private void sendEmail(SlingHttpServletRequest request){ log.info("GOING TO SEND EMAIL(ServletMailer)"); ArrayList<InternetAddress> emailRecipients = new ArrayList<InternetAddress>(); MessageGatewayService messageGatewayService = Activator.getMessageGatewayService(); MessageGateway<HtmlEmail> messageGateway = messageGatewayService.getGateway(HtmlEmail.class); org.apache.sling.api.resource.Resource templateRsrc = request.getResourceResolver().getResource(this.getClass().getResource(TEMPLATE).toString()); if (templateRsrc.getChild("file") != null) { templateRsrc = templateRsrc.getChild("file"); } if (templateRsrc == null) { log.error("Missing template: " + TEMPLATE); return; } try{ MailTemplate mailTemplate = MailTemplate.create(templateRsrc.getPath(), templateRsrc.getResourceResolver().adaptoTo(Session.class)); //this is line 81 HtmlEmail email = mailTemplate.getEmail(StrLookup.mapLookup(prefs), HtmlEmail.class); email.addTo(RECIPIENT); email.setCharset("UTF-8"); messageGateway.send(email); }catch(Exception e){ log.error("Error creating MailTemplate, HtmlEmail or sending mail."); } } }

According to documentation, the org.apache.sling.api.resource.Resource's method getResourceResolver() should return an org.apache.sling.api.resource.ResourceResolver object which implements the Adaptable interface that has the adaptTo methhod, so my code should work. So why do I still get that error?

Thanks for any help

1 Accepted Solution

Avatar

Correct answer by
Level 7

Looks like there is just a typo in you code. adaptoTo() should be adaptTo()
Good luck
/Johan

View solution in original post

2 Replies

Avatar

Correct answer by
Level 7

Looks like there is just a typo in you code. adaptoTo() should be adaptTo()
Good luck
/Johan

Avatar

Level 4

Ojjis wrote...

Looks like there is just a typo in you code. adaptoTo() should be adaptTo()
Good luck
/Johan

 

Such a stupid mistake and I spent two hours looking for solution... Thank you!