如何在@SlingServlet 注释中调用使用 "resourceType" 而不是 "paths" 的 Sling Servlet
How to invoke Sling Servlet that uses "resourceType" instead of "paths" in @SlingServlet annotation
如何调用在@SlingServlet 中使用 "resourceType" 属性 的 Sling Servlet?当我使用 "paths" 时,我只是用 ajax 调用来调用它,但我不确定如果我将我的 servlet 更改为使用 "resourceType" 而不是 "paths" 该怎么办。 (我正在为学习目的进行更改)
我还在学习CQ5、Sling等
我的 servlet 紧随其后。
package com.mypackage.weather;
import org.apache.sling.api.resource.*;
import org.apache.sling.commons.osgi.PropertiesUtil;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import javax.servlet.ServletException;
import java.io.BufferedReader;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingServlet(
name="Weatherservlet",
//paths="/bin/Weather",
resourceType="OpenWeather/components/page/contentPage",
methods="GET",
metatype=true)
@Properties({
@org.apache.felix.scr.annotations.Property(name="WeatherServlet", description="Get JSON String weather info", value="mitch weather"),
@org.apache.felix.scr.annotations.Property(name = "apikey", label = "The api key", value = "d8e39388b0bc54a62ffc6b385639b3dc") // register the api key in the OSGi console
})
/**
* Handles requests for getting weather information from OpenWeatherMap.org. returns the information as a JSon string.
*/
public class WeatherServlet extends SlingSafeMethodsServlet {
private static final String SERVER = "localhost:4502";
private static final String RESOURCE_PATH = "/content/OpenWeather";
private String apikey = "";
private String location = "";
private ResourceResolver resourceResolver;
private Logger logger = LoggerFactory.getLogger(WeatherServlet.class);
@Override
public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
logger.info("Reconfigured Weather Servlet");
getWeather(request, response);
}
/**
* Gets current weather information from OpenWeatherMap.org API
* @param request
* @param response
* @throws IOException
*/
public void getWeather(SlingHttpServletRequest request, SlingHttpServletResponse response) {
logger.info("api key: " + apikey);
location = request.getParameter("city");
logger.info("city sent: " + location);
String urlString = "http://api.openweathermap.org/data/2.5/weather?q=" + location + "&units=imperial&APPID=" + apikey;
logger.info("urlString: " + urlString);
URL url = null;
HttpURLConnection connection = null;
int responseCode = -9;
String result = "";
logger.info("Before call to Open Weather");
long startTime = System.currentTimeMillis();
try {
url = new URL(urlString);
logger.info("url: " + url);
connection = (HttpURLConnection) url.openConnection();
logger.info("Connection: " + connection);
connection.setRequestMethod("GET");
responseCode = connection.getResponseCode();
logger.info("After calling Open Weather");
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
logger.info("reader: " + reader);
result = reader.readLine();
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
logger.info("Elapsed Time is... " + elapsedTime);
logger.info("result: " + result);
PrintWriter writer = response.getWriter();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
writer.write(result);
} catch (MalformedURLException e) {
logger.info("MalformedURL");
e.printStackTrace();
} catch (IOException e) {
logger.info("IOException!!!!!!!!");
e.printStackTrace();
logger.info("Cause: " + e.getCause());
}
}
protected void activate(ComponentContext context)
{
apikey = PropertiesUtil.toString(context.getProperties().get("apikey"), "d8e39388b0bc54a62ffc6b385639b3dc"); // Get the api key from the OSGi console
System.out.println("weather servlet activated");
}
}
不是对 servlet 中的路径进行 ajax 调用,而是对组件进行 ajax 调用。如果您希望 servlet 使用 resourceType,则 servlet 应该有一个额外的扩展配置 属性 (sling.servlet.extensions ).此配置让您 运行 在资源(特定资源类型)而不是全局资源上下文中的 servlet。
让我举个例子来解释。考虑一个页面 content/home.html 在路径处有一个 foo 组件 (resourceType="/apps/someproject/components/foo) /par/foo 。通常在页面上,将使用 .html 选择器请求组件,资源将由默认脚本呈现 (foo.jsp). 让我们添加一个带有以下注释的 servlet
@SlingServlet(
name="Weatherservlet",
extensions = "pdf",
resourceType="someproject/components/foo",
methods="GET",
metatype=true)
这将给出资源的 pdf 表示。
对 /content/home/jcr:content/par/foo.pdf
的 GET 请求将由 servlet 而不是 foo.jsp 处理。
request.getResource()
在 servlet 的 doGet
中将 return 组件资源。
路径配置将覆盖资源类型配置。
如何调用在@SlingServlet 中使用 "resourceType" 属性 的 Sling Servlet?当我使用 "paths" 时,我只是用 ajax 调用来调用它,但我不确定如果我将我的 servlet 更改为使用 "resourceType" 而不是 "paths" 该怎么办。 (我正在为学习目的进行更改)
我还在学习CQ5、Sling等
我的 servlet 紧随其后。
package com.mypackage.weather;
import org.apache.sling.api.resource.*;
import org.apache.sling.commons.osgi.PropertiesUtil;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import javax.servlet.ServletException;
import java.io.BufferedReader;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingServlet(
name="Weatherservlet",
//paths="/bin/Weather",
resourceType="OpenWeather/components/page/contentPage",
methods="GET",
metatype=true)
@Properties({
@org.apache.felix.scr.annotations.Property(name="WeatherServlet", description="Get JSON String weather info", value="mitch weather"),
@org.apache.felix.scr.annotations.Property(name = "apikey", label = "The api key", value = "d8e39388b0bc54a62ffc6b385639b3dc") // register the api key in the OSGi console
})
/**
* Handles requests for getting weather information from OpenWeatherMap.org. returns the information as a JSon string.
*/
public class WeatherServlet extends SlingSafeMethodsServlet {
private static final String SERVER = "localhost:4502";
private static final String RESOURCE_PATH = "/content/OpenWeather";
private String apikey = "";
private String location = "";
private ResourceResolver resourceResolver;
private Logger logger = LoggerFactory.getLogger(WeatherServlet.class);
@Override
public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
logger.info("Reconfigured Weather Servlet");
getWeather(request, response);
}
/**
* Gets current weather information from OpenWeatherMap.org API
* @param request
* @param response
* @throws IOException
*/
public void getWeather(SlingHttpServletRequest request, SlingHttpServletResponse response) {
logger.info("api key: " + apikey);
location = request.getParameter("city");
logger.info("city sent: " + location);
String urlString = "http://api.openweathermap.org/data/2.5/weather?q=" + location + "&units=imperial&APPID=" + apikey;
logger.info("urlString: " + urlString);
URL url = null;
HttpURLConnection connection = null;
int responseCode = -9;
String result = "";
logger.info("Before call to Open Weather");
long startTime = System.currentTimeMillis();
try {
url = new URL(urlString);
logger.info("url: " + url);
connection = (HttpURLConnection) url.openConnection();
logger.info("Connection: " + connection);
connection.setRequestMethod("GET");
responseCode = connection.getResponseCode();
logger.info("After calling Open Weather");
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
logger.info("reader: " + reader);
result = reader.readLine();
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
logger.info("Elapsed Time is... " + elapsedTime);
logger.info("result: " + result);
PrintWriter writer = response.getWriter();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
writer.write(result);
} catch (MalformedURLException e) {
logger.info("MalformedURL");
e.printStackTrace();
} catch (IOException e) {
logger.info("IOException!!!!!!!!");
e.printStackTrace();
logger.info("Cause: " + e.getCause());
}
}
protected void activate(ComponentContext context)
{
apikey = PropertiesUtil.toString(context.getProperties().get("apikey"), "d8e39388b0bc54a62ffc6b385639b3dc"); // Get the api key from the OSGi console
System.out.println("weather servlet activated");
}
}
不是对 servlet 中的路径进行 ajax 调用,而是对组件进行 ajax 调用。如果您希望 servlet 使用 resourceType,则 servlet 应该有一个额外的扩展配置 属性 (sling.servlet.extensions ).此配置让您 运行 在资源(特定资源类型)而不是全局资源上下文中的 servlet。
让我举个例子来解释。考虑一个页面 content/home.html 在路径处有一个 foo 组件 (resourceType="/apps/someproject/components/foo) /par/foo 。通常在页面上,将使用 .html 选择器请求组件,资源将由默认脚本呈现 (foo.jsp). 让我们添加一个带有以下注释的 servlet
@SlingServlet(
name="Weatherservlet",
extensions = "pdf",
resourceType="someproject/components/foo",
methods="GET",
metatype=true)
这将给出资源的 pdf 表示。
对 /content/home/jcr:content/par/foo.pdf
的 GET 请求将由 servlet 而不是 foo.jsp 处理。
request.getResource()
在 servlet 的 doGet
中将 return 组件资源。
路径配置将覆盖资源类型配置。