PageRenderTime 21ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_atom/resources/resource_atom_entry.erl

http://github.com/zotonic/zotonic
Erlang | 124 lines | 73 code | 29 blank | 22 comment | 0 complexity | ba639720e76f6fa919dedaec64811bcf MD5 | raw file
  1%% @author Arjan Scherpenisse <arjan@scherpenisse.net>
  2%% @copyright 2009 Arjan Scherpenisse
  3%% Date: 2009-10-02
  4%% @doc Atom entry resource.
  5
  6%% Copyright 2009 Arjan Scherpenisse
  7%%
  8%% Licensed under the Apache License, Version 2.0 (the "License");
  9%% you may not use this file except in compliance with the License.
 10%% You may obtain a copy of the License at
 11%% 
 12%%     http://www.apache.org/licenses/LICENSE-2.0
 13%% 
 14%% Unless required by applicable law or agreed to in writing, software
 15%% distributed under the License is distributed on an "AS IS" BASIS,
 16%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 17%% See the License for the specific language governing permissions and
 18%% limitations under the License.
 19
 20-module(resource_atom_entry).
 21-author("Arjan Scherpenisse <arjan@scherpenisse.net>").
 22
 23-export([
 24    init/1,
 25    service_available/2,
 26    is_authorized/2,
 27    allowed_methods/2,
 28    encodings_provided/2,
 29	resource_exists/2,
 30	last_modified/2,
 31	expires/2,
 32	content_types_provided/2,
 33	charsets_provided/2,
 34	provide_content/2
 35]).
 36
 37
 38-include_lib("webmachine_resource.hrl").
 39-include_lib("include/zotonic.hrl").
 40
 41%% Let cached versions expire in an hour.
 42-define(MAX_AGE, 3600).
 43
 44
 45init(DispatchArgs) ->
 46    {ok, DispatchArgs}.
 47
 48service_available(ReqData, DispatchArgs) when is_list(DispatchArgs) ->
 49    Context  = z_context:new(ReqData, ?MODULE),
 50    Context1 = z_context:set(DispatchArgs, Context),
 51    Context2 = z_context:ensure_qs(Context1),
 52    ?WM_REPLY(true, Context2).
 53
 54
 55allowed_methods(ReqData, Context) ->
 56    {['HEAD', 'GET'], ReqData, Context}.
 57
 58
 59charsets_provided(ReqData, Context) ->
 60    {[{"utf-8", fun(X) -> X end}], ReqData, Context}.
 61
 62
 63encodings_provided(ReqData, Context) ->
 64    {[
 65        {"identity", fun(X) -> X end}, 
 66        {"gzip", fun(X) -> zlib:gzip(X) end}
 67    ], ReqData, Context}.
 68
 69
 70content_types_provided(ReqData, Context) ->
 71    {[{"application/atom+xml;type=entry", provide_content},
 72      {"application/atom+xml", provide_content}],
 73     ReqData, Context}.
 74
 75
 76%% @doc Check if the id in the request (or dispatch conf) exists.
 77resource_exists(ReqData, Context) ->
 78    Context1  = ?WM_REQ(ReqData, Context),
 79    ContextQs = z_context:ensure_qs(Context1),
 80    try
 81        ?WM_REPLY(m_rsc:exists(get_id(ContextQs), ContextQs), ContextQs)
 82    catch
 83        _:_ -> ?WM_REPLY(false, ContextQs)
 84    end.
 85
 86
 87%% @doc Check if the current user is allowed to view the resource. 
 88is_authorized(ReqData, Context) ->
 89    Context1  = ?WM_REQ(ReqData, Context),
 90    ContextQs = z_context:ensure_qs(Context1),
 91    z_acl:wm_is_authorized(view, get_id(ContextQs), ContextQs).
 92
 93
 94last_modified(ReqData, Context) ->
 95    RD1 = wrq:set_resp_header("Cache-Control", "public, max-age="++integer_to_list(?MAX_AGE), ReqData),
 96    Modified = m_rsc:p(get_id(Context), modified, Context),
 97    Context1 = z_context:set(last_modified, Modified, Context),
 98    {Modified, RD1, Context1}.
 99
100
101expires(ReqData, State) ->
102    NowSecs = calendar:datetime_to_gregorian_seconds(calendar:universal_time()),
103    {calendar:gregorian_seconds_to_datetime(NowSecs + ?MAX_AGE), ReqData, State}.
104
105
106provide_content(ReqData, Context) ->
107    Context1 = z_context:set_reqdata(ReqData, Context),
108    Id = get_id(Context1),
109    RscExport = m_rsc_export:full(Id, Context1),
110    Content = atom_convert:resource_to_atom(RscExport),
111    ?WM_REPLY(Content, Context1).
112
113
114%% @doc Fetch the id from the request or the dispatch configuration.
115%% @spec get_id(Context) -> int() | false
116get_id(Context) ->
117    ReqId = case z_context:get(id, Context) of
118        undefined -> z_context:get_q("id", Context);
119        ConfId -> ConfId
120    end,
121    case m_rsc:name_to_id(ReqId, Context) of
122        {ok, RscId} -> RscId;
123        _ -> false
124    end.