| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 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 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 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 | |
|
| 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 | |
} |