Example of creating a web service to search for an item

This example shows how to complete a search in the Java™ API and illustrates how to return an array of items rather than a single value.

About this task

This example also shows how to add authentication to your web service, and how to use the PIMWebServicesContextFactory to obtain your initial PIMContext.

Procedure

  1. Write the Java code. The Java API offers a JDBC-like search capability, which provides an equivalent to the JDBC statement and result set that most Java programmers who use JDBC are familiar with. The following sample Java code demonstrates the use of the Java API to create a Java API search and process the results.
    public class SearchService
    {
    
        /**
         * Search a catalog for a specific spec's attribute.
         * 
         * @param catalog
         *            the catalog to search
         * @param spec
         *            the spec to search
         * @param attribute
         *            the attribute to search for
         * @return the result set as an array of Strings
         */
        public String[] search(String catalog, String spec, String attribute)
        {
            Context context = null;
            SearchResultSet searchResultSet = null;
            String[] searchResults = null;
    
            try
            {
                // Obtain a PIM Context and a Search Manager
                context = PIMWebServicesContextFactory.getContext();
            }
            catch (Exception e1)
            {
                e1.printStackTrace();
            }
    
            // Build a search string and obtain a Search query instance
            String queryString = "select item ['" + spec + "/" + attribute + "'] from catalog('" + catalog + "')";
            System.out.println("Query string built as : " + queryString);
    
            SearchQuery query;
            try
            {
                query = context.createSearchQuery(queryString);
    
                // Execute the query
                searchResultSet = query.execute();
    
                // Process the Search result Set
                if (searchResultSet != null && searchResultSet.size() > 0)
                {
                    String currentResult = null;
                    searchResults = new String[searchResultSet.size()];
                    int resultsIndex = 0;
    
                    while (searchResultSet.next())
                    {
                        currentResult = searchResultSet.getString(1);
                        System.out.println("Result : " + resultsIndex + " is : " + currentResult);
    
                        // Add to the result set
                        searchResults[resultsIndex] = currentResult;
                        resultsIndex++;
                    }
                }
                else
                {
                    // No results so return empty array
                    searchResults = new String[] {};
                }
            }
            catch (PIMInternalException pie)
            {
                pie.printStackTrace();
            }
            catch (PIMAuthorizationException pae)
            {
                pae.printStackTrace();
            }
            catch (PIMSearchException pse)
            {
                pse.printStackTrace();
            }
    
            return searchResults;
        }
    
    }
    
  2. Deploy the user .jar file.
  3. Register the web service in IBM® Product Master.
    1. Access your Product Master instance and log in.
      For example: http://yourWPCserver:yourWPCport/utils/enterLogin.jsp.
    2. Click Collaboration Manager > Web Services > Web Service Console > New.
    3. Provide the following values:
      • Web Service Name: Provide a name. For example, SearchService.
      • WSDL: Type <definition/> to indicate that Product Master should generate the WSDL for you.
      • Web Service Implementation: Select Java.
      • Java Implementation Class: Type the Java class of your web service. In the above example, you would type com.acme.javawbs.SearchService.
      • Deployed: Select this check box. Clearing this selection enables you to store web services in an inactive (unusable) state.
      • Authenticated: Select this check box to have the user name, company, and password that is specified when the web service is started.
    4. Click Save. If you get an error similar to: Unable to verify Java implementation class, and you have no typographical errors in your fully qualified Java class name, then you did not successfully deploy your Java class through the user .jar mechanism. Return to Step 2 and check whether your user .jar appears in your class path.
      For example, ps -ef | grep java and check the Java process for Product Master.
  4. Test starting your web service. After you retrieve a query, you can now use the execute() method to run the query and process the returned search result set:
    http://YourWPCServer:YourWPCPort /services/ServiceName?method=search&spec=YourSpecName&attribute=YourAttributeName&catalogName=YourCatalogName
    Where YourWPCServer and YourWPCPort is the URL for accessing your Product Master system, ServiceName is the name that you gave the web service when you deployed it to Product Master, and YourSpecNameYourAttributeName, and YourCatalogName are the spec, attribute, and catalog that you want to query.

    Because you selected Authenticated, an Axis login dialog opens. Enter the appropriate user name and password, for example: User name: Joe@Acme Password: passw0rd. The output in the browser window returns three items from the catalog.