/** * Appends all the data of the {@link InputStream}. * * @param stream * {@link InputStream} of data to append. * @throws HttpException * If fails to append the {@link InputStream} data. */ public void append(InputStream stream) throws HttpException { try { for (int value = stream.read(); value != -1; value = stream.read()) { this.digest.update((byte) value); } } catch (IOException ex) { throw new HttpException(ex); } }
/** * Loads the value onto the object. * * @param object * {@link Object} to have value loaded on it. * @param method * {@link Method} to load the values. * @param parameters * Values to be loaded into the {@link Method}. * @throws HttpException * If fails to load the values. */ public static void loadValue(Object object, Method method, Object... parameters) throws HttpException { try { // Load the value method.invoke(object, parameters); } catch (InvocationTargetException ex) { // Propagate cause of invocation escalation Throwable cause = ex.getCause(); if (cause instanceof HttpException) { throw (HttpException) cause; } else { throw new HttpException(cause); } } catch (Exception ex) { throw new HttpException(ex); } }
@Override public String getAuthenticationScheme() { // Use the first available authentication scheme for (HttpAccessControl accessControl : this.httpAccessControls) { if (accessControl != null) { return accessControl.getAuthenticationScheme(); } } // As here, invalid as should always have access control throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, null, "No " + HttpAccessControl.class.getSimpleName() + " available"); }
@Override public Principal getPrincipal() { // Use the first available authentication scheme for (HttpAccessControl accessControl : this.httpAccessControls) { if (accessControl != null) { return accessControl.getPrincipal(); } } // As here, invalid as should always have access control throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, null, "No " + HttpAccessControl.class.getSimpleName() + " available"); }
@Override public String createApplicationClientPath(T values) throws HttpException { try { // Obtain the string builder to create the path StringBuilder buffer = stringBuilder.get(); buffer.setLength(0); // Generate the path for (int i = 0; i < this.segments.length; i++) { Segment<T> segment = this.segments[i]; segment.write(values, buffer); } // Return the path return buffer.toString(); } catch (Exception ex) { throw new HttpException(ex); } }
/** * Initiate. * * @param algorithm * Algorithm. * @throws HttpException * If fails to initiate for algorithm. */ public Digest(String algorithm) throws HttpException { this.digest = CredentialStoreUtil.createDigest(algorithm); if (this.digest == null) { throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, null, "Unable to create Digest for algorithm '" + algorithm + "'"); } }
@Override public Set<String> retrieveRoles() throws HttpException { try { // Search for the groups NamingEnumeration<SearchResult> groupResults = JndiLdapCredentialStore.this.context.search( JndiLdapCredentialStore.this.rolesSearchBaseDn, "(&(objectClass=groupOfNames)(member=" + this.entryDn + "))", null); // Obtain the set of roles Set<String> roles = new HashSet<String>(); for (; groupResults.hasMore();) { SearchResult group = groupResults.next(); // Obtain the role from the group String role = (String) group.getAttributes().get("ou").get(); // Add role to listing roles.add(role); } // Return the roles return roles; } catch (NamingException ex) { throw new HttpException(ex); } } }
throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, null, "No authentication credentials for " + this.entryDn); throw new HttpException(ex);
@Override @SuppressWarnings("unchecked") public T parse(ServerHttpConnection connection) throws HttpException { try { return (T) mapper.readValue(connection.getRequest().getEntity(), javaType); } catch (IOException ex) { throw new HttpException(ex); } } };
@Override public CredentialEntry retrieveCredentialEntry(String userId, String realm) throws HttpException { try { // Search for the credential entry NamingEnumeration<SearchResult> searchResults = this.context.search(this.entrySearchBaseDn, "(&(objectClass=inetOrgPerson)(uid=" + userId + "))", null); if (!searchResults.hasMore()) { return null; // entry not found } SearchResult result = searchResults.next(); // Obtain the attributes String entryDn = result.getNameInNamespace(); // Create and return the credential entry return new JndiLdapCredentialEntry(entryDn); } catch (NamingException ex) { throw new HttpException(ex); } }
@Override public void write(ServerWriter writer, boolean isDefaultCharset, Object bean, ServerHttpConnection connection, String templatePath) throws HttpException { try { // Use pre-encoded content if using default charset if (isDefaultCharset) { // Provide pre-encoded content writer.write(this.encodedContent); } else { // Provide the content (with appropriate charset) writer.write(this.textContent); } } catch (IOException ex) { throw new HttpException(ex); } }
@Override public void administer(AdministrationContext<HttpAccessControl, None, None> context) throws HttpException { // Easy access roles String[] anyRoles = HttpAccessAdministrationSource.this.anyRoles; String[] allRoles = HttpAccessAdministrationSource.this.allRoles; // Obtain the HTTP Access Control HttpAccessControl[] accessControls = context.getExtensions(); // Provide means to load access control HttpAccessControl accessControl = accessControls[0]; // Ensure has access if (!accessControl.isAccess(anyRoles, allRoles)) { throw new HttpException(HttpStatus.FORBIDDEN); } // As here, has access } }
@Override public void loadObjects(ObjectRegistry<Indexed> registry) throws Throwable { this.objectRegistry = registry; // Obtain the server HTTP connection ServerHttpConnection connection = (ServerHttpConnection) registry .getObject(DefaultHttpAuthenticationManagedObjectSource.this.httpSecurityNames.length); // Determine the HTTP authentications to use int[] httpAuthenticationIndexes = DefaultHttpAuthenticationManagedObjectSource.this.negotiator .getHandler(connection.getRequest()); if (httpAuthenticationIndexes == null) { throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, null, "No " + HttpAuthentication.class.getSimpleName() + " negotiated for default " + HttpAuthentication.class.getSimpleName()); } // Load the HTTP authentications this.httpAuthentications = new HttpAuthentication[httpAuthenticationIndexes.length]; for (int i = 0; i < httpAuthenticationIndexes.length; i++) { int httpAuthenticationIndex = httpAuthenticationIndexes[i]; this.httpAuthentications[i] = (HttpAuthentication<?>) registry.getObject(httpAuthenticationIndex); } }
throw (HttpException) ex; } else { throw new HttpException(ex);
@Override public void write(ServerWriter writer, boolean isDefaultCharset, Object bean, ServerHttpConnection connection, String templatePath) throws HttpException { // If no bean, then no value to output if (bean == null) { return; } // Obtain the property value from bean Object value = this.valueRetriever.retrieveValue(bean, this.propertyName); // Obtain the text value to write as content String propertyTextValue = (value == null ? "" : value.toString()); try { // Write out the value if (this.isEscaped) { // Write the escaped value StringEscapeUtils.ESCAPE_HTML4.translate(propertyTextValue, writer); } else { // Write the raw value writer.write(propertyTextValue); } } catch (IOException ex) { throw new HttpException(ex); } }
@Override public void send(T object) throws HttpException { this.context.run(() -> { // Lazy obtain the content type cache if (this.contentTypeCache == null) { this.contentTypeCache = ObjectResponseManagedObjectSource.this.objectNegotiator .getHandler(this.connection.getRequest()); } // Ensure have acceptable content type if (this.contentTypeCache == null) { throw new HttpException(HttpStatus.NOT_ACCEPTABLE, ObjectResponseManagedObjectSource.this.notAcceptableHeaders, null); } // Handle the object handleObject(object, this.contentTypeCache, OBJECT_RESPONDER_FACTORY, this.connection); return null; }); } }
@Override public HttpAccessControl getAccessControl() throws AuthenticationRequiredException, HttpException { return this.authenticationContext.run(() -> { // Propagate potential failure if (this.escalation != null) { if (this.escalation instanceof HttpException) { throw (HttpException) this.escalation; } else { throw new HttpException(this.escalation); } } // Ensure have access control if (this.accessControl == null) { throw new AuthenticationRequiredException(this.authenticationContext.getQualifier()); } // Return the access control return this.accessControl; }); }
/** * ==================== ManagedFunction ====================== */ @Override public Object execute(ManagedFunctionContext<HttpHandleRedirectDependencies, Indexed> context) throws Throwable { // Obtain the dependencies HttpRequestCookie cookie = (HttpRequestCookie) context.getObject(HttpHandleRedirectDependencies.COOKIE); ServerHttpConnection connection = (ServerHttpConnection) context .getObject(HttpHandleRedirectDependencies.SERVER_HTTP_CONNECTION); HttpRequestState requestState = (HttpRequestState) context .getObject(HttpHandleRedirectDependencies.REQUEST_STATE); HttpSession session = (HttpSession) context.getObject(HttpHandleRedirectDependencies.SESSION); // Obtain the serialised state (ensuring the intended state) SerialisedRequestState serialisable = (SerialisedRequestState) session .getAttribute(HttpRedirectFunction.SESSION_ATTRIBUTE_REDIRECT_MOMENTO); if (serialisable == null) { throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, null, "No momento for redirection"); } String serialisableIdentifier = String.valueOf(serialisable.identifier); if (!serialisableIdentifier.equals(cookie.getValue())) { throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, null, "Redirect momento invalid version"); } // Re-instate the request state HttpRequestStateManagedObjectSource.importHttpRequestState(serialisable.momento, requestState); // Undertake routing (with re-instated request state) return this.router.route(connection, context); }
/** * Obtains the {@link MockAccessControl}. * * @return {@link MockAccessControl}. */ public MockAccessControl getAccessControl() { return this.authenticationContext.run(() -> { // Propagate potential failure if (this.escalation != null) { if (this.escalation instanceof HttpException) { throw (HttpException) this.escalation; } else { throw new HttpException(this.escalation); } } // Ensure have access control if (this.accessControl == null) { throw new AuthenticationRequiredException(this.authenticationContext.getQualifier()); } // Return the access control return this.accessControl; }); }
@Override public void write(ServerWriter writer, boolean isDefaultCharset, Object bean, ServerHttpConnection connection, String templatePath) throws HttpException { // Obtain the link path (determining if require secure link) if (this.isLinkSecure && (!connection.isSecure())) { templatePath = connection.getServerLocation().createClientUrl(this.isLinkSecure, templatePath); } try { // Write the content writer.write(templatePath); writer.write(this.linkPathSuffix); } catch (IOException ex) { throw new HttpException(ex); } }