_policyManager = (PolicyManager) getServletContext().getAttribute(PolicyManager.class.getName());
}
protected void doPublish(SipServletRequest publish) throws ServletException, IOException
{
Presentity presentity = null;
try
{
String event = publish.getHeader(Constants.EVENT);
if (event == null || !event.equals(_presence.getName()))
{
SipServletResponse response = publish.createResponse(SipServletResponse.SC_BAD_EVENT);
response.addHeader(Constants.ALLOW_EVENTS, _presence.getName());
response.send();
return;
}
String uri = URIUtil.toCanonical(publish.getRequestURI());
int expires = publish.getExpires();
if (expires != -1)
{
if(expires != 0)
{
if (expires < _presence.getMinStateExpires())
{
SipServletResponse response = publish.createResponse(SipServletResponse.SC_INTERVAL_TOO_BRIEF);
response.addHeader(Constants.MIN_EXPIRES, Integer.toString(_presence.getMinStateExpires()));
response.send();
return;
}
else if (expires > _presence.getMaxStateExpires())
expires = _presence.getMaxStateExpires();
}
}
else
{
expires = _presence.getDefaultStateExpires();
}
String contentType = publish.getContentType();
List<String> supported = _presence.getSupportedContentTypes();
if (contentType != null && !(supported.contains(contentType)))
{
SipServletResponse response = publish.createResponse(SipServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
for (String s : supported)
response.addHeader(Constants.ACCEPT, s);
response.send();
return;
}
byte[] raw = publish.getRawContent();
Object content = null;
if (raw != null)
{
if (contentType == null)
{
SipServletResponse response = publish.createResponse(SipServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
for (String s : supported)
response.addHeader(Constants.ACCEPT, s);
response.send();
return;
}
ContentHandler<?> handler = _presence.getContentHandler(contentType);
try
{
content = handler.getContent(raw);
}
catch (Exception e)
{
if (_log.isDebugEnabled())
_log.debug("invalid content {}", e);
publish.createResponse(SipServletResponse.SC_BAD_REQUEST).send();
return;
}
}
String etag = publish.getHeader(Constants.SIP_IF_MATCH);
long now = System.currentTimeMillis();
presentity = _presence.get(uri);
if (etag == null)
{
if (content == null)
{
publish.createResponse(SipServletResponse.SC_BAD_REQUEST).send();
return;
}
SoftState state = presentity.addState(contentType, content, now + expires*1000);
if (_log.isDebugEnabled())
_log.debug("added state {} to presentity {}", state.getETag(), presentity);
SipServletResponse response = publish.createResponse(SipServletResponse.SC_OK);
response.setExpires(expires);
response.setHeader(Constants.SIP_ETAG, state.getETag());
response.send();
}
else
{
SoftState state = presentity.getState(etag);
if (state == null)
{
publish.createResponse(SipServletResponse.SC_CONDITIONAL_REQUEST_FAILED).send();
return;
}
if (expires == 0)
{
if (_log.isDebugEnabled())
_log.debug("removed state {} from presentity {}", state.getETag(), presentity);
presentity.removeState(etag);
}
else
{
if (content != null)
{
presentity.modifyState(state, contentType, content, now + expires*1000);
if (_log.isDebugEnabled())
_log.debug("modified state {} (new etag {}) from presentity {}",
new Object[] {etag, state.getETag(), presentity});
}
else
{
presentity.refreshState(state, now + expires*1000);
if (_log.isDebugEnabled())
_log.debug("refreshed state {} (new etag {}) from presentity {}",
new Object[] {etag, state.getETag(), presentity});
}
}