Coverage Report - org.semispace.ws.TokenWsSpaceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
TokenWsSpaceImpl
0%
0/31
0%
0/4
1.364
 
 1  
 /*
 2  
  * ============================================================================
 3  
  *
 4  
  *  File:     TokenWsSpaceImpl.java
 5  
  *----------------------------------------------------------------------------
 6  
  *
 7  
  * Copyright 2008 Erlend Nossum
 8  
  *
 9  
  * Licensed under the Apache License, Version 2.0 (the "License"); 
 10  
  * you may not use this file except in compliance with the License. 
 11  
  * You may obtain a copy of the License at 
 12  
  *
 13  
  *   http://www.apache.org/licenses/LICENSE-2.0
 14  
  *
 15  
  * Unless required by applicable law or agreed to in writing, software
 16  
  * distributed under the License is distributed on an "AS IS" BASIS,
 17  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 18  
  * See the License for the specific language governing permissions and 
 19  
  * limitations under the License.
 20  
  *
 21  
  *  Description:  See javadoc below
 22  
  *
 23  
  *  Created:      Mar 16, 2008
 24  
  * ============================================================================ 
 25  
  */
 26  
 
 27  
 package org.semispace.ws;
 28  
 
 29  
 import javax.jws.WebService;
 30  
 
 31  
 import org.slf4j.Logger;
 32  
 import org.slf4j.LoggerFactory;
 33  
 import org.semispace.SemiSpace;
 34  
 
 35  
 /**
 36  
  * All methods will throw RuntimeException if the token is not found to be OK. Otherwise, the operation in question is
 37  
  * just relayed to WsSpaceImpl.
 38  
  * <p>
 39  
  * You need to set both the space and the authenticator.
 40  
  * </p>
 41  
  * 
 42  
  * @see WsSpaceImpl
 43  
  */
 44  
 @WebService(endpointInterface = "org.semispace.ws.TokenWsSpace")
 45  
 public class TokenWsSpaceImpl implements TokenWsSpace {
 46  0
     private static final Logger log = LoggerFactory.getLogger(TokenWsSpaceImpl.class);
 47  
 
 48  
     private WsSpaceImpl wsspace;
 49  
 
 50  
     private TokenAuthenticator auth;
 51  
 
 52  0
     public TokenWsSpaceImpl() {
 53  0
         wsspace = new WsSpaceImpl();
 54  0
     }
 55  
 
 56  
     public void setTokenAuthenticator(TokenAuthenticator auth) {
 57  0
         this.auth = auth;
 58  0
     }
 59  
 
 60  
     /** For the benefit of spring */
 61  
     public void setSpace(SemiSpace space) {
 62  0
         wsspace.setSpace(space);
 63  0
     }
 64  
 
 65  
     public String login(String username, String password) {
 66  0
         makeSureAuthIsPresent();
 67  0
         return auth.authenticate(username, password);
 68  
     }
 69  
 
 70  
     private void makeSureAuthIsPresent() {
 71  0
         if (auth == null) {
 72  0
             String error = "Erroneous initizalization - need TokenAuthenticator.";
 73  0
             log.error(error);
 74  0
             throw new RuntimeException(error);
 75  
         }
 76  0
     }
 77  
 
 78  
     public String read(String token, String template, long queryLifeMs) {
 79  0
         makeSureAuthIsOk(token);
 80  0
         return wsspace.read(template, queryLifeMs);
 81  
     }
 82  
 
 83  
     private void makeSureAuthIsOk(String token) {
 84  0
         makeSureAuthIsPresent();
 85  0
         if (!auth.isTokenValid(token)) {
 86  0
             log.warn("Space tried used with token which is not OK. Token: " + token);
 87  0
             throw new RuntimeException("Token incorrect");
 88  
         }
 89  0
     }
 90  
 
 91  
     public String readIfExists(String token, String template) {
 92  0
         makeSureAuthIsOk(token);
 93  0
         return wsspace.readIfExists(template);
 94  
     }
 95  
 
 96  
     public String take(String token, String template, long queryLifeMs) {
 97  0
         makeSureAuthIsOk(token);
 98  0
         return wsspace.take( template,queryLifeMs);
 99  
     }
 100  
 
 101  
     public String takeIfExists(String token, String template) {
 102  0
         makeSureAuthIsOk(token);
 103  0
         return wsspace.takeIfExists(template);
 104  
     }
 105  
 
 106  
     public void write(String token, String contents, long timeToLiveMs) {
 107  0
         makeSureAuthIsOk(token);
 108  0
         wsspace.write(contents, timeToLiveMs);
 109  0
     }
 110  
 
 111  
 }