diff options
author | Endi Sukma Dewata <edewata@redhat.com> | 2012-08-11 05:09:02 -0500 |
---|---|---|
committer | Endi Sukma Dewata <edewata@redhat.com> | 2012-08-15 12:07:08 -0500 |
commit | d41848a5881db3d12f65e078b945829f1fe296a5 (patch) | |
tree | b98a85b38f18cf6121d39164ae6280ef957be9e5 /base/common/src/com/netscape/cms/servlet/base/PKIService.java | |
parent | 56768d1bd274228a391ac8a4272ab9ccf5ac3e51 (diff) | |
download | pki-d41848a5881db3d12f65e078b945829f1fe296a5.tar.gz pki-d41848a5881db3d12f65e078b945829f1fe296a5.tar.xz pki-d41848a5881db3d12f65e078b945829f1fe296a5.zip |
Cleaned up REST server class names.
The REST server classes have been renamed for better clarity
and consistency.
Ticket #259
Diffstat (limited to 'base/common/src/com/netscape/cms/servlet/base/PKIService.java')
-rw-r--r-- | base/common/src/com/netscape/cms/servlet/base/PKIService.java | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/base/common/src/com/netscape/cms/servlet/base/PKIService.java b/base/common/src/com/netscape/cms/servlet/base/PKIService.java new file mode 100644 index 000000000..de92d290a --- /dev/null +++ b/base/common/src/com/netscape/cms/servlet/base/PKIService.java @@ -0,0 +1,168 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2012 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- +package com.netscape.cms.servlet.base; + +import java.lang.reflect.Method; +import java.security.cert.CertificateEncodingException; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.FormParam; +import javax.ws.rs.core.CacheControl; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.EntityTag; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.Request; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.ResponseBuilder; +import javax.ws.rs.core.UriInfo; + +import com.netscape.certsrv.apps.CMS; +import com.netscape.certsrv.logging.IAuditor; +import com.netscape.certsrv.logging.ILogger; +import com.netscape.cms.servlet.cert.model.CertificateData; + +/** + * Base class for CMS RESTful resources + * + * @author alee + * + */ +public class PKIService { + + public static final String HEADER = "-----BEGIN NEW CERTIFICATE REQUEST-----"; + public static final String TRAILER = "-----END NEW CERTIFICATE REQUEST-----"; + + // caching parameters + public static final int DEFAULT_LONG_CACHE_LIFETIME = 1000; + + @Context + protected UriInfo uriInfo; + + @Context + protected HttpHeaders headers; + + @Context + protected Request request; + + @Context + protected HttpServletRequest servletRequest; + + public ILogger logger = CMS.getLogger(); + public IAuditor auditor = CMS.getAuditor(); + + public Response createOKResponse(Object object) { + return Response.ok(object).build(); + } + + public Response sendConditionalGetResponse(int ctime, Object object) { + CacheControl cc = new CacheControl(); + cc.setMaxAge(ctime); + EntityTag tag = new EntityTag(Integer.toString(object.hashCode())); + + ResponseBuilder builder = request.evaluatePreconditions(tag); + if (builder != null) { + builder.cacheControl(cc); + return builder.build(); + } + + builder = Response.ok(object); + builder.cacheControl(cc); + builder.tag(tag); + return builder.build(); + } + + public CertificateData createCertificateData(org.mozilla.jss.crypto.X509Certificate cert) + throws CertificateEncodingException { + CertificateData data = new CertificateData(); + String b64 = HEADER + CMS.BtoA(cert.getEncoded()) + TRAILER; + data.setEncoded(b64); + return data; + } + + public Locale getLocale() { + + if (headers == null) return Locale.getDefault(); + + List<Locale> locales = headers.getAcceptableLanguages(); + if (locales == null || locales.isEmpty()) return Locale.getDefault(); + + return locales.get(0); + } + + public String getUserMessage(String messageId, String... params) { + return CMS.getUserMessage(getLocale(), messageId, params); + } + + public void log(int source, int level, String message) { + + if (logger == null) return; + + logger.log(ILogger.EV_SYSTEM, + null, + source, + level, + getClass().getSimpleName() + ": " + message); + } + + public void audit(String message, String scope, String type, String id, Map<String, String> params, String status) { + + if (auditor == null) return; + + String auditMessage = CMS.getLogMessage( + message, + auditor.getSubjectID(), + status, + auditor.getParamString(scope, type, id, params)); + + auditor.log(auditMessage); + } + + /** + * Get the values of the fields annotated with @FormParam. + */ + public Map<String, String> getParams(Object object) { + + Map<String, String> map = new HashMap<String, String>(); + + // for each fields in the object + for (Method method : object.getClass().getMethods()) { + FormParam element = method.getAnnotation(FormParam.class); + if (element == null) continue; + + String name = element.value(); + + try { + // get the value from the object + Object value = method.invoke(object); + + // put the value in the map + map.put(name, value == null ? null : value.toString()); + + } catch (Exception e) { + // ignore inaccessible fields + e.printStackTrace(); + } + } + + return map; + } +} |