0
votes

I'm trying to use the Google Custom Search API to, obviously, search in google. I've made this Java Agent in Lotus Notes.

The Main Class:

import java.util.List;
import lotus.domino.AgentBase;
import com.google.api.services.customsearch.model.Result;
public class JavaAgent extends AgentBase {
    public void NotesMain() {
        GoogleSearchClient gsc = new GoogleSearchClient();
        String searchKeyWord = "test";
        List<Result> resultList =    gsc.getSearchResult(searchKeyWord);
        if(resultList != null && resultList.size() > 0){
            for(Result result: resultList){
                System.out.println(result.getHtmlTitle());
                System.out.println(result.getFormattedUrl());
                System.out.println("----------------------------------------");
            }
        }
    }
}

And that's the GoogleSearchClient class:

import java.util.Collections;
import java.util.List;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.services.customsearch.Customsearch;
import com.google.api.services.customsearch.model.Result;
import com.google.api.services.customsearch.model.Search;
public class GoogleSearchClient {
    public List<Result> getSearchResult(String keyword){
        String GOOGLE_SEARCH_URL = https://www.googleapis.com/customsearch/v1?";
        //api key
        String API_KEY = "xxxxxxxxxxxxxxxxxxxxx";
        //custom search engine ID
        String SEARCH_ENGINE_ID = "xxxxxxxxxx:xxxxxxxxxxxx";
        String FINAL_URL= GOOGLE_SEARCH_URL + "key=" + API_KEY + "&cx=" + SEARCH_ENGINE_ID;
        // Set up the HTTP transport and JSON factory
        HttpTransport httpTransport = new NetHttpTransport();
        JsonFactory jsonFactory = new com.google.api.client.json.jackson2.JacksonFactory();
        //HttpRequestInitializer initializer = (HttpRequestInitializer)new CommonGoogleClientRequestInitializer(API_KEY);
        Customsearch customsearch = new Customsearch(httpTransport, jsonFactory,null);

        List<Result> resultList = Collections.emptyList();
        try {
            Customsearch.Cse.List list = customsearch.cse().list(keyword);
            list.setKey(API_KEY);
            list.setCx(SEARCH_ENGINE_ID);
            //num results per page
            //list.setNum(2L);

            //for pagination
            list.setStart(10L);
            Search results = list.execute();
            resultList = results.getItems();

        }catch (Exception e) {
            e.printStackTrace();
        }
        return resultList;
    }
}

I've got the code here.

This returns me this Exception:

java.security.AccessControlException: Access denied (java.lang.reflect.ReflectPermission suppressAccessChecks)
    at java.security.AccessController.throwACE(AccessController.java:100)
    at java.security.AccessController.checkPermission(AccessController.java:174)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:544)
    at COM.ibm.JEmpower.applet.AppletSecurity.superDotCheckPermission(AppletSecurity.java:1449)
    at COM.ibm.JEmpower.applet.AppletSecurity.checkPermission(AppletSecurity.java:1617)
    at COM.ibm.JEmpower.applet.AppletSecurity.checkPermission(AppletSecurity.java:1464)
    at java.lang.reflect.AccessibleObject.setAccessible(AccessibleObject.java:118)
    at com.google.api.client.util.FieldInfo.of(FieldInfo.java:97)
    at com.google.api.client.util.ClassInfo.<init>(ClassInfo.java:172)
    at com.google.api.client.util.ClassInfo.of(ClassInfo.java:90)
    at com.google.api.client.util.GenericData.<init>(GenericData.java:79)
    at com.google.api.client.util.GenericData.<init>(GenericData.java:61)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.<init>(AbstractGoogleClientRequest.java:109)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.<init>(AbstractGoogleJsonClientRequest.java:57)
    at com.google.api.services.customsearch.CustomsearchRequest.<init>(CustomsearchRequest.java:43)
    at com.google.api.services.customsearch.Customsearch$Cse$List.<init>(Customsearch.java:178)
    at com.google.api.services.customsearch.Customsearch$Cse.list(Customsearch.java:154)
    at GoogleSearchClient.getSearchResult(Unknown Source)
    at JavaAgent.NotesMain(Unknown Source)
    at lotus.domino.AgentBase.runNotes(Unknown Source)
    at lotus.domino.NotesThread.run(Unknown Source)

I've digged this Exception in the internet and I've understood that the JVM doesn't think that I have the privileges and tried some things.

  1. I added this permissions below in the "Java.policy" archive in my local machine and in the server, but it doesn't work.

    grant { permission java.util.PropertyPermission "http.keepAlive", "read, write"; };
    grant { permission java.security.AllPermission; } 
    
  2. I would try this but my Software Version is 9.

  3. I tryed this same code in Eclipse and it worked just fine, so I think that's a Notes Security configuration that is wrong. I have to do in Lotus Notes because I have to save the informations in forms etc.

  4. I changed the Runtime security Level to 3 (Allow restricted operations with full administration rights)

Any ideas that how can I go through this?

1

1 Answers

0
votes

When I was working on WS-Security for my Web Service Consumer in Lotus, I got the same error. I found out that I can avoid this by using AccessController.doPrivileged method in my .jar file. So, you need to create separate .jar in your IDE and use it in your Lotus Agent.
Here is example of using AccessController.doPrivileged with your code:

import java.util.Collections;
import java.util.List;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.services.customsearch.Customsearch;
import com.google.api.services.customsearch.model.Result;
import com.google.api.services.customsearch.model.Search;
public class GoogleSearchClient {
    public List<Result> getSearchResult(final String keyword){
        String GOOGLE_SEARCH_URL = https://www.googleapis.com/customsearch/v1?";
        //api key
        final String API_KEY = "xxxxxxxxxxxxxxxxxxxxx";
        //custom search engine ID
        final String SEARCH_ENGINE_ID = "xxxxxxxxxx:xxxxxxxxxxxx";
        String FINAL_URL= GOOGLE_SEARCH_URL + "key=" + API_KEY + "&cx=" + SEARCH_ENGINE_ID;
        // Set up the HTTP transport and JSON factory
        HttpTransport httpTransport = new NetHttpTransport();
        JsonFactory jsonFactory = new com.google.api.client.json.jackson2.JacksonFactory();
        //HttpRequestInitializer initializer = (HttpRequestInitializer)new CommonGoogleClientRequestInitializer(API_KEY);
        final Customsearch customsearch = new Customsearch(httpTransport, jsonFactory,null);

        return AccessController.doPrivileged(
                new PrivilegedAction<List<Result>>() {
            @Override
            public List<Result> run() {
                List<Result> resultList = Collections.emptyList();
                try {
                    Customsearch.Cse.List list = customsearch.cse().list(keyword);
                    list.setKey(API_KEY);
                    list.setCx(SEARCH_ENGINE_ID);
                    //num results per page
                    //list.setNum(2L);

                    //for pagination
                    list.setStart(10L);
                    Search results = list.execute();
                    resultList = results.getItems();

                }catch (Exception e) {
                    e.printStackTrace();
                }
                return resultList;
            }
        });
    }
}