[ Pobierz całość w formacie PDF ]
.equals(ACTION_SEARCH)) {//Set response not to be cachedres.setHeader("Expires","Tues, 01 Jan 1980 00:00:00 GMT");searchLDAP(req,out,isInternal);} else if (cmd.equals(ACTION_DETAIL)) {if (!isInternal) {errorMessage(out,"Not authorized!");} else {detailEdit(req,out,true); //display only}} else if (cmd.equals(ACTION_GETCERT)) {binaryLDAP (req, res, certAttr, "Certificate","application/x-x509-email-cert" );} else if (cmd.equals(ACTION_GETPHOTO)) {if (!isInternal) {res.setContentType("text/html");out = res.getWriter();errorMessage(out,"Not authorized!");} else {binaryLDAP (req, res, photoAttr, "Photograph","image/jpeg" );}} else if (cmd.equals(ACTION_EDIT)) {if (!isInternal) {res.setContentType("text/html");out = res.getWriter();errorMessage(out,"Not authorized!");} else {detailEdit(req,out,false); //Edit mode}} else if (cmd.equals(ACTION_DOEDIT)) {// Process user valuesmodifyEntry(req,out); 450 Servlets and LDAP} else if (cmd.equals(ACTION_PWD)) {sendPwdPage(req,out,isInternal);} else if (cmd.equals(ACTION_DOPWD)) {modifyPwd(req,out);} else {res.setContentType("text/html");sendSrchPage(out,isInternal,req.getRequestURI());}return;}The following routine is the handler for all search requests.It parses the searchstring and determines the type of search (by name, telephone number, etc.).The searchstrings are also modified to include an asterisk at the end to allow matches with valuesthat begin with the search string.The search by name also includes an LDAP  soundslike search: "~=" (see Table 5-1).Once the search filter is validated and constructed,we obtain a connection from the pool and issue the search request.The search utilizesserver-side sorting (if supported by the server) and calls generateRow to build eachrow of the result set./*** Handle search requests to the servlet.* Retrieve arguments from the req and transmit HTML* back to the client.Build an LDAP search request* using server-side sorting when possible.If not* possible, do client-side sorting (in the servlet).** @param req the HTTP request* @param out stream back to the client* @param isInternal true if client* is internal to organization*/private void searchLDAP( HttpServletRequest req,PrintWriter out,boolean isInternal ) {// Get search argumentsString srchType =readFormData(req,FLDNAME_SRCHKIND).toLowerCase();String srchString = readFormData(req,FLDNAME_SRCHSTRING);LDAPSearchResults res=null;LDAPConnection ld;String appendmsg = ""; Our Phone Book Servlet 451String srch = "";String[] myAttrs;String uri = req.getRequestURI();boolean ssort = true; // Use server-side sorting// Build full URLString url = req.getScheme()+ "://" +req.getServerName();// Assume standard port numbers.If not, uncomment below.// + ":" + req.getServerPort();myAttrs = isInternal ? intAttrs : extAttrs;// Use the attribute name for telephone number in case the// external number is in a different attribute from// the internal one// myAttrs[1] is the telephone number attribute nameif ( srchType.equals("all") ) {srch = "(|(cn=" + srchString + "*)" +"(sn=" + srchString + "*)" +"(givenName=" + srchString+ "*)" +"("+myAttrs[1]+"=" + srchString + "*)" +")";} else if ( srchType.equals("cn") ) {srch = "(|(cn=" + srchString + ")(cn=" +srchString + "*)" +"(cn~="+srchString+"))";} else if ( srchType.equals("mail") ) {srch = "(|(mail="+srchString+")" +"(mail="+srchString+"*)" +")";} else if ( srchType.equals("telephoneNumber") ) {srch = "(|("+myAttrs[1]+"="+srchString+")" +"("+myAttrs[1]+"="+srchString+"*)" +")";} else if ( srchType.equals("facsimiletelephoneNumber") ) {srch = "(|(facsimiletelephoneNumber="+srchString+")" +"(facsimiletelephoneNumber="+srchString+"*)" +")";}if ( (srch.length() == 0) || srchString.length() == 0) { 452 Servlets and LDAP// Not a valid search typesendSrchPage(out,isInternal,req.getRequestURI());return;}ld = getLDAPConn();try {LDAPSearchConstraints cons =ld.getSearchConstraints();// Block until all results are incons.setBatchSize( 0 );if ( ssort ) {// Use server-side sortingLDAPSortKey sortL = new LDAPSortKey("sn");LDAPSortKey sortF = new LDAPSortKey("givenName");LDAPSortKey sortC = new LDAPSortKey("cn");LDAPSortKey[] sortOrder = {sortL,sortF,sortC};//Create the server controlLDAPSortControl sortCtl =new LDAPSortControl(sortOrder,false);cons.setServerControls(sortCtl);}res = ld.search( srchRoot,scope,srch,myAttrs,false,cons);// If sorting on server, then check if sort workedif ( ssort ) {LDAPControl[] rControls =ld.getResponseControls();if ( rControls != null ) {// Was there a server-side sort error?LDAPSortControl response = null;for( int i = 0; i ");out.println(""+orgName+" Search "+"Results");out.println("");out.println("");out.println("");out.println("Name");out.println("Phone");out.println("Email");out.println("");out.println("");int rowcnt = 0;while ( res.hasMoreElements() ) { Our Phone Book Servlet 455try {// Next directory entryLDAPEntry entry = res.next();generateRow( out,LIST_COLOR[++rowcnt%2],myAttrs,entry,uri,url,isInternal);} catch ( LDAPReferralException e ) {; // Ignore referrals} catch ( LDAPException e ) {System.err.println( "Entry read error: " +e.toString() );// There may be valid results as wellcontinue;}}out.println("");if ( appendmsg.length() > 0 ) {out.println("Search error:"+appendmsg);out.println("");}out.println("");out.println("Questions or comments email webmaster"+"");out.println("");out.println("");closeConnection(ld);return;}There are many references in the servlet code to a JavaScript function calleddoNothing.This function is there to allow a result to be returned from clicking on aURL or Submit button.It does not perform any function other than to prevent theaction from occurring.We intercept the action and call JavaScript functions instead.A sample of the output from a search is shown in Figure 13-5.The photographicon next to the entry for Barbara Jensen indicates that she has stored a photo in thedirectory.Clicking on this icon will open a separate window with the photograph.Thisaction is handled by the routine that follows, which we use for all binary data types./*** Return binary data in an HTTP stream;* retrieve arguments from the req** @param req the HTTP request 456 Servlets and LDAPFIGURE 13-5.Sample search results.* @param res the HTTP response* @param attr name of attribute in the LDAP directory to* return* @param name "friendly" name of the attribute to use for* transmitting an error message* @param mime MIME type for this binary data* @exception ServletException* @exception IOException*/private void binaryLDAP( HttpServletRequest req,HttpServletResponse res,String attr,String name, String mime )throws ServletException, IOException {LDAPConnection ld;LDAPEntry theEntry = null;String[] attrs = {attr};int err = 0;byte[] theData = null; Our Phone Book Servlet 457// Get form argumentsString theDN =readFormData(req,FLDNAME_DN).toLowerCase();if ( theDN.length() == 0 ) {PrintWriter out = res.getWriter();errorMessage(out,"Error with Distingushed Name");return;}theDN = LDAPUrl.decode(theDN);ld = getLDAPConn();try {theEntry = ld.read(theDN,attrs);theData = getBinaryValue(theEntry,attr);if ( theData == null ) {PrintWriter out = res.getWriter();errorMessage(out, name + " for " + theDN +" not found!");err = 1;}} catch ( LDAPException e ) {PrintWriter out = res [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • drakonia.opx.pl
  • Linki