This file is indexed.

/usr/share/perl5/Data/HAL.pm is in libdata-hal-perl 1.000-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package Data::HAL;
use strictures;
use boolean qw(false true);
use Clone qw(clone);
use Data::HAL::Link qw();
use Data::HAL::URI qw();
use Data::HAL::URI::NamespaceMap qw();
use Data::Visitor::Callback qw();
use failures qw(Data::HAL::InvalidJSON);
use HTTP::Headers::Util qw(join_header_words);
use JSON qw();
use Moo; # has
use Safe::Isa qw($_isa);
use Scalar::Util qw(reftype);
use Types::Standard qw(ArrayRef Bool HashRef InstanceOf Str);

our $VERSION = '1.000';

my $uri_from_str = sub {
    my ($val) = @_;
    return $val->$_isa('Data::HAL::URI') ? $val : Data::HAL::URI->new($val);
};

has('embedded', is => 'rw', isa => ArrayRef[InstanceOf['Data::HAL']]);
has('links',    is => 'rw', isa => ArrayRef[InstanceOf['Data::HAL::Link']]);
has('resource', is => 'rw', isa => HashRef, default => sub {return {};});
has('relation', is => 'rw', isa => InstanceOf['Data::HAL::URI'], coerce => $uri_from_str);
has('_nsmap',   is => 'rw', isa => InstanceOf['Data::HAL::URI::NamespaceMap']);
has('_recursing', is => 'ro', isa => Bool);

sub BUILD {
    my ($self) = @_;
    $self->_expand_curies unless $self->_recursing;
    return;
}

sub from_json {
    my ($self, $json, $relation) = @_;
    my $nested = clone JSON::from_json($json);
    failure::Data::HAL::InvalidJSON->throw('not a JSON object') unless reftype $nested eq reftype {};
    return $self->_from_nested($self->_boolify($nested), $relation)->_expand_curies;
}

sub _boolify {
    my ($self, $nested) = @_;
    my $visited = Data::Visitor::Callback->new(
        object => sub {
            my (undef, $val) = @_;
            if (JSON::is_bool($val)) {
                $val = false if $val == JSON::false;
                $val = true if $val == JSON::true;
            }
            return $val;
        }
    )->visit($nested);
    return $visited;
}

sub _from_nested {
    my ($class, $nested, $relation, $_recursing) = @_;
    my $embedded = delete $nested->{_embedded};
    my $links = delete $nested->{_links};
    my $self = $class->new(
        $relation ? (relation => $relation) : (),
        resource => $nested,
        $_recursing ? (_recursing => $_recursing) : (),
    );
    if ($embedded) {
        $self->embedded([]);
        for my $k (keys %{ $embedded }) {
            push @{ $self->embedded }, (reftype $embedded->{$k} eq reftype [])
                ? map { $self->_from_nested($_, $k, 1) } @{ $embedded->{$k} }
                : $self->_from_nested($embedded->{$k}, $k, 1);
        }
    }
    if ($links) {
        $self->links([]);
        for my $k (keys %{ $links }) {
            push @{ $self->links }, (reftype $links->{$k} eq reftype [])
                ? map { Data::HAL::Link->new(%{ $_ }, relation => $k) } @{ $links->{$k} }
                : Data::HAL::Link->new(%{ $links->{$k} }, relation => $k);
        }
    }
    return $self;
}

sub _expand_curies {
    my ($self) = @_;
    my $nsmap = Data::HAL::URI::NamespaceMap->new;
    if ($self->links) {
        for my $l (@{ $self->links }) {
            if ('curies' eq $l->relation->as_string) {
                $nsmap->add_mapping($l->name, $l->href);
            }
        }
    }
    $self->_nsmap($nsmap);
    $self->_recurse_curies($self);
    return $self;
}

sub _recurse_curies {
    my ($self, $root) = @_;
    if ($self->relation) {
        my $uri = $root->_nsmap->uri($self->relation->as_string);
        if ($uri) {
            $self->relation(Data::HAL::URI->new(
                uri => $uri->uri,
                _original => $self->relation->_original,
            ));
        }
    }
    if ($self->links) {
        for my $l (@{ $self->links }) {
            my $uri = $root->_nsmap->uri($l->relation->as_string($root));
            next unless $uri;
            $l->relation(Data::HAL::URI->new(
                uri => $uri->uri,
                _original => $l->relation->_original,
            ));
        }
    }
    if ($self->embedded) {
        for my $e (@{ $self->embedded }) {
            $e->_recurse_curies($root);
        }
    }
    return $self;
}

sub _to_nested {
    my ($self, $root) = @_;
    if ($self->relation) {
        $self->relation(Data::HAL::URI->new(
            uri => URI->new($self->relation->_original),
            _original => $self->relation->_original,
        ));
    }
    my $hal = clone $self->resource;
    for my $prop (qw(links embedded)) {
        if ($self->$prop) {
            for my $p (@{ $self->$prop }) {
                my ($nested, $r) = $p->_to_nested($root);
                if (exists $hal->{"_$prop"}{$r}) {
                    if (reftype $hal->{"_$prop"}{$r} eq reftype []) {
                        push @{ $hal->{"_$prop"}{$r} }, $nested;
                    } else {
                        my $attr = delete $hal->{"_$prop"}{$r};
                        push @{ $hal->{"_$prop"}{$r} }, $attr, $nested;
                    }
                } else {
                    $hal->{"_$prop"}{$r} = $nested;
                }
            }
        }
    }
    return($hal, $self->relation ? $self->relation->as_string : ());
}

sub TO_JSON {
    my ($self) = @_;
    my ($nested) = $self->_to_nested($self);
    my $visited = Data::Visitor::Callback->new(
        boolean => sub {
            my (undef, $val) = @_;
            return $val ? JSON::true : JSON::false;
        }
    )->visit($nested);
    return $visited;
}

sub as_json {
    my ($self) = @_;
    return JSON::to_json($self->TO_JSON, { canonical => 1, pretty => 1, utf8 => 1 });
}

sub http_headers {
    my ($self) = @_;
    my @headers;
    if ($self->links) {
        if (my ($profile_link) = grep { 'profile' eq $_->relation->as_string } @{ $self->links }) {
            push @headers, 'Content-Type' => join_header_words(
                'application/hal+json' => undef, profile => $profile_link->href->as_string
            );
        } else {
            push @headers, 'Content-Type' => 'application/hal+json';
        }
        push @headers,
            map { (Link => $_->as_http_link_value) }
            grep { 'curies' ne $_->relation->as_string }
            @{ $self->links };
    }
    return @headers;
}

1;

__END__

=encoding UTF-8

=head1 NAME

Data::HAL - Hypertext Application Language resource

=head1 VERSION

This document describes Data::HAL version 1.000

=head1 SYNOPSIS

    use Data::HAL qw();
    use Data::HAL::Link qw();
    {
        my $hal = Data::HAL->from_json($json_str);
        my $resource_member_data_href = $hal->resource;
        my $links_aref = $hal->links;
        my $embedded_resources_aref = $hal->embedded;
    }

    {
        my $hal = Data::HAL->new(
            resource => {foo => 23, bar => 42},
            links    => [Data::HAL::Link->new(relation => 'self', href => '/')],
        );

        my $json_str = $hal->as_json;
        # {
        #    "_links" : {
        #       "self" : {
        #          "href" : "/"
        #       }
        #    },
        #    "bar" : 42,
        #    "foo" : 23
        # }

        my @headers = $hal->http_headers;
        # (
        #     'Content-Type' => 'application/hal+json',
        #     'Link' => '</>;rel="self"'
        # )
    }

=head1 DESCRIPTION

HAL is a format you can use in your hypermedia API.

=head2 Introduction

This section is completely quoted from the specification:

There is an emergence of non-HTML HTTP applications ("Web APIs")
which use hyperlinks to direct clients around their resources.

The JSON Hypertext Application Language (HAL) is a standard which
establishes conventions for expressing hypermedia controls, such as
links, with JSON.

HAL is a generic media type with which Web APIs can be developed and
exposed as series of links.  Clients of these APIs can select links
by their link relation type and traverse them in order to progress
through the application.

HAL's conventions result in a uniform interface for serving and
consuming hypermedia, enabling the creation of general-purpose
libraries that can be re-used on any API utilising HAL.

The primary design goals of HAL are generality and simplicity.  HAL
can be applied to many different domains, and imposes the minimal
amount of structure necessary to cover the key requirements of a
hypermedia Web API.

=head2 Conformance

The author claims to conform with L<http://tools.ietf.org/html/draft-kelly-json-hal-06>, published 2013-10-03.

=head1 INTERFACE

=head2 Composition

None.

=head2 Constructors

=head3 C<from_json>

    Data::HAL->from_json($json_str)

Takes a HAL+JSON document as string and returns a C<Data::HAL> object.

=head3 C<new>

    Data::HAL->new(
        resource => {foo => 23, bar => 42},
        links    => [Data::HAL::Link->new(relation => 'self', href => '/')]
    )

Default L<Moo> constructor, returns a C<Data::HAL> object.

=head2 Attributes

=head3 C<embedded>

Type C<ArrayRef[Data::HAL]>,
L<< embedded resource objects|http://tools.ietf.org/html/draft-kelly-json-hal#section-4.1.2 >>

=head3 C<links>

Type C<ArrayRef[Data::HAL::Link]>,
L<< link objects|http://tools.ietf.org/html/draft-kelly-json-hal#section-4.1.1 >>

=head3 C<resource>

Type C<HashRef>, member data
L<< representing the current state of the resource|http://tools.ietf.org/html/draft-kelly-json-hal#section-4 >>

=head3 C<relation>

Type L<Data::HAL::URI>,
L<< identifier of the semantics of a link|http://tools.ietf.org/html/rfc5988#section-4 >>

Perl strings are coerced to the L<Data::HAL::URI> type.

A stand-alone HAL+JSON document, when deserialised, will not have this attribute set in the root resource since nothing
is linking to the document.

=head2 Methods

=head3 C<TO_JSON>

Serialisation hook for the L<JSON> (or compatible) module.

This method is not intended to be called directly from your code. Instead call L</as_json> or
C<< JSON::to_json $hal, { convert_blessed => 1 } >> or similar.

=head3 C<as_json>

Returns the resource object serialised as a HAL+JSON document string.

=head3 C<http_headers>

Returns a list of pairs of HTTP message headers. The keys are field name strings and the values are field content
strings. B<Warning>: since field names can repeat, assigning this list to a hash loses information.

The list is suitable as input for e.g. the
L<< C<headers> accessor in HTTP::Headers|HTTP::Headers/$h->header( $f1 => $v1, $f2 => $v2, ... ) >>
or the L<< C<headers> attribute in Plack::Response|Plack::Response/headers >>.

=head4 C<Content-Type>

The value is C<application/hal+json>, perhaps with a C<profile> parameter.

=head4 C<Link>

See L<Data::HAL::Link/as_http_link_value>.

=head2 Exports

None.

=head1 DIAGNOSTICS

=head2 C<not a JSON object>

The L</from_json> constructor throws this exception of type C<failure::Data::HAL::InvalidJSON> when the JSON input is a
malformed HAL+JSON document.

=head1 CONFIGURATION AND ENVIRONMENT

Requires no configuration files or environment variables.

=head1 DEPENDENCIES

See meta file in the source distribution.

=head1 INCOMPATIBILITIES

None reported.

=head1 BUGS AND LIMITATIONS

Please report any bugs or feature requests to C<bug-data-hal@rt.cpan.org>, or through the web interface at
L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-HAL>.

=head2 tight coupling to JSON

It is currently difficult to exert control over the specifics of (de)serialisation, perhaps put the (de)serialisers
into attributes?

=head2 error type is an attribute, not a class name

It is not worth it to design an error class hierarchy for a single error.

=head2 Data::HAL::URI::NamespaceMap is undocumented

It is used only internally.

=head1 TO DO

=over

=item make everything cache-friendly

=item non-standard accessors for link objects

=item support §8.3. cache pattern

=item support HAL XML

=back

=head1 SEE ALSO

L<AtomPub|http://enwp.org/AtomPub>, the more mature, featureful hypermedia protocol

=head1 AUTHOR

Lars Dɪᴇᴄᴋᴏᴡ C<< <daxim@cpan.org> >>

=head1 LICENCE AND COPYRIGHT

Copyright © 2013 Lars Dɪᴇᴄᴋᴏᴡ C<< <daxim@cpan.org> >>

This library is free software; you can redistribute it and/or modify it
under the same terms as Perl 5.18.0.

=head2 Disclaimer of warranty

This library 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.