Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.wookie.helpers;
 
 
 
 import  org.apache.wookie.util.HashGenerator;
Helper methods for working with API keys
 
 public class WidgetKeyManager{
   static Logger _logger = Logger.getLogger(WidgetKeyManager.class.getName());

Revoke the key matching the supplied key value

Parameters:
value the api key value
Returns:
true if the key was successfully revoked
 
 	public static boolean revokeKey(String value){
 		if (value == nullreturn false;
 		if (value.trim().equals("")) return false;
 		value = value.trim();
 		IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
 		IApiKey[] key = persistenceManager.findByValue(IApiKey.class"value", value);
 		if (key == null || key.length !=1) return false;
 		return revokeKey(persistenceManager, key[0]);
 	}

Revoke a key

Parameters:
key the key to revoke
 
 	private static boolean revokeKey(IPersistenceManager persistenceManagerIApiKey key){
 		if (key == nullreturn false;
 		persistenceManager.delete(key);
 		return true;
 	}

Registers a new API key and notifies the requestor via email of the key values.

Parameters:
request the request to create a new API key
email the email address of the registering party
localizedMessages the set of localized messages
Throws:
EmailException if there is a problem sending the email notification about this key
SystemUnavailableException if there is a problem generating the key
 
 	public static void createKey(HttpServletRequest requestString emailMessages localizedMessagesthrows SystemUnavailableExceptionEmailException {
 	    
 	    IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
 		IApiKey key = persistenceManager.newInstance(IApiKey.class);
 		key.setEmail(email);
 
 		// generate a nonce
 		RandomGUID r = new RandomGUID();
 		String nonce = "nonce-" + r.toString();				 //$NON-NLS-1$
 
 		// now use SHA hash on the nonce				
 		String hashKey = HashGenerator.getInstance().encrypt(nonce+email);	
 
 		// get rid of any chars that might upset a url...
 		hashKey = hashKey.replaceAll("="".eq."); //$NON-NLS-1$ //$NON-NLS-2$
 		hashKey = hashKey.replaceAll("\\?"".qu."); //$NON-NLS-1$ //$NON-NLS-2$
 		hashKey = hashKey.replaceAll("&"".am."); //$NON-NLS-1$ //$NON-NLS-2$
 		hashKey = hashKey.replaceAll("\\+"".pl."); //$NON-NLS-1$ //$NON-NLS-2$
 
 		key.setValue(hashKey);
 		persistenceManager.save(key);
 		
 		String message = localizedMessages.getString("WidgetKeyManager.0")+hashKey+" \n";  //$NON-NLS-1$//$NON-NLS-2$
 		message+="\n" + localizedMessages.getString("WidgetKeyManager.1"); //$NON-NLS-1$ //$NON-NLS-2$
 		
 		Configuration properties = (Configuration) request.getSession().getServletContext().getAttribute("properties"); //$NON-NLS-1$
 		
		String server = properties.getString("widget.email.server");
		int port = properties.getInt("widget.email.port");
		String contact = properties.getString("widget.email.contact");
		sendEmail(server, port, contact, email, message, properties.getString("widget.email.username"), properties.getString("widget.email.password")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}

Send email.

Parameters:
mailserver - the SMTP mail server address
from
to
message
Throws:
Exception
	private static void sendEmail(String mailserverint portString fromString toString messageString usernameString passwordthrows EmailException {	
		Email email = new SimpleEmail();
		email.setDebug(false); // true if you want to debug
        email.setHostName(mailserver);
        if (username != null) {
			email.setAuthentication(username, password);
	        email.getMailSession().getProperties().put(
	           "mail.smtp.starttls.enable""true");
		}
        email.setFrom(from, "Wookie Server");
        email.setSubject("Wookie API Key");
        email.setMsg(message);
        email.addTo(to);
        email.send();
	}
New to GrepCode? Check out our FAQ X