无法从独立的Java程序

时间:2016-03-20 05:06:57

标签: cq5

我有一个CQ5 servlet,我可以在AEM环境中调用这个servlet。我需要从独立的java程序访问这个servlet。这个servlet返回一个json。

我的CQ5 servlet如下:

@Component(immediate = true, metatype = false)
@Service
@Properties({
        @org.apache.felix.scr.annotations.Property(name = "sling.servlet.paths", value = "/bin/jsonServlet"),
        @org.apache.felix.scr.annotations.Property(name = "sling.servlet.methods", value = "GET") })
public class JsonServlet extends SlingAllMethodsServlet {


    private Resource resource;

    @Reference
    private DataSourcePool dataSourceService;

    protected void doGet(SlingHttpServletRequest request,
            SlingHttpServletResponse response) throws ServletException,
            IOException {

        response.setHeader(CONTENTTYPE, APPLICATIONJSON);
        DataSource dataSource;
        try {
            log.info("***Inside JsonServlet***");
            JSONObject courseObject = new JSONObject();
            dataSource = (DataSource) dataSourceService.getDataSource("IDATASOURCE");
            List<Map<String, String>> dbColValsList = DownloadCoursesDBHelper.fetchCoursesLite(dataSource);         
            int courseIndex=0;
            for(Map<String, String> propertyVals:dbColValsList){
                JSONObject propertyList = generatePropertyList(propertyVals);
                if (propertyList != null) {
                    courseObject.put(COURSE+courseIndex++, propertyList);
                }
            }
            response.setCharacterEncoding("UTF-8");
            response.getWriter().println(courseObject);

        } catch (Exception e) {
            log.error("Error"
                    + e.getMessage());
        }
    }

我访问此servlet的独立java程序如下所示:

package com.aem.servlet;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class InvokeSlingServlet {

    public static void main(String[] args) {


        // Create an HTTPClient object
        HttpClient client = new HttpClient();
        HttpMethod method = new GetMethod("http://localhost:4502/bin/jsonServlet");

        client.getState().setCredentials(new AuthScope("localhost",4502), new UsernamePasswordCredentials("admin", "admin"));
        //method.setDoAuthentication( true );
        try {
            int statusCode = client.executeMethod(method);

            if (statusCode != HttpStatus.SC_OK) {
                System.out.println("HTTP Method failed: "
                        + method.getStatusLine());
            }

            // Read the response body.
            byte[] responseBody = method.getResponseBody();

            System.out.println("INVOKED THE AEM SLING SERVLET!!! " + new String(responseBody));

        } catch (Exception e) {
            System.out.println("ERROR: " + e.getMessage());
        }

    }
}   

当我运行java程序时,我得到一个回复​​: HTTP方法失败:找不到HTTP / 1.1 404

请告诉我是否需要在AEM环境中配置任何设置,还需要对我的独立java程序进行任何更改。

0 个答案:

没有答案