[[headless-access]]
= Headless access

:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
:page-partial:

This section tackles the topic of enabling access to an Apache Isis application directly, or at least, not through either the xref:vw:ROOT:about.adoc[Wicket] or xref:vro:ROOT:about.adoc[Restful] viewers.

There are a number of use-cases:

* implementing a REST end point that needs to reach into the Apache Isis runtime

** this could be a custom UI
** this could be to integrate from other systems, eg a subscriber on a pub/sub event bus, pushing changes through an Apache Isis domain model.

* calling from Quartz for a background batch process

Note that the calling thread runs in the same process space as the Apache Isis domain object model (must be physically linked to the JAR files containing the domain classes).

The xref:docs:starters:simpleapp.adoc[SimpleApp] starter app demonstrates the pattern with a custom UI controller:

[source,java]
----
@RestController
@RequiredArgsConstructor
class CustomController {

    private final InteractionService interactionService;
    private final TransactionalProcessor transactionalProcessor;
    private final SimpleObjects simpleObjects;

    @GetMapping("/custom/simpleObjects")
    List<SimpleObject> all() {
        return callAuthenticated(newAuthentication(), () -> simpleObjects.listAll())
                .optionalElseFail() // <.>
                .orElse(Collections.emptyList()); // <.>
    }

    private SimpleAuthentication newAuthentication() {
        return SimpleAuthentication.validOf(UserMemento.ofName("sven")); // <.>
    }

    private <T> Result<T> callAuthenticated(
            final Authentication authentication,
            final Callable<T> task) {

        return interactionFactory.callAuthenticated(
                authentication,
                () -> transactionalProcessor
                      .callWithinCurrentTransactionElseCreateNew(task));
    }
}
----
<.> re-throws exception that has occurred, if any
<.> handles null case, if required
<.> a more sophisticated implementation could inspect the HTTP request

The API described here is reasonably low-level, allowing code to interact very directly with the Apache Isis metamodel and runtime.
Such callers should be considered trusted: they do not (by default) honour any business rules eg implicit in the Isis annotations or hide/disable/validate methods.

