Beispiel #1
0
func (p *InMemoryDataStore) Encode(w io.Writer) os.Error {
	v, err := jsonhelper.MarshalWithOptions(p, dm.UTC_DATETIME_FORMAT)
	if err != nil {
		return err
	}
	return json.NewEncoder(w).Encode(v)
}
func (p *GeneratePrivateKeyRequestHandler) ContentTypesProvided(req wm.Request, cxt wm.Context) ([]wm.MediaTypeHandler, wm.Request, wm.Context, int, error) {
	gpkc := cxt.(GeneratePrivateKeyContext)
	user := gpkc.User()
	consumer := gpkc.Consumer()
	var userId, consumerId string
	if user != nil {
		userId = user.Id
	}
	if consumer != nil {
		consumerId = consumer.Id
	}
	accessKey, err := p.authDS.StoreAccessKey(dm.NewAccessKey(userId, consumerId))
	gpkc.SetAccessKey(accessKey)
	obj := make(map[string]interface{})
	if user != nil {
		obj["user_id"] = user.Id
		obj["username"] = user.Username
		obj["name"] = user.Name
	}
	if consumer != nil {
		obj["consumer_id"] = consumer.Id
		obj["consumer_short_name"] = consumer.ShortName
	}
	if accessKey != nil {
		obj["access_key_id"] = accessKey.Id
		obj["private_key"] = accessKey.PrivateKey
	}
	theobj, _ := jsonhelper.MarshalWithOptions(obj, dm.UTC_DATETIME_FORMAT)
	jsonObj, _ := theobj.(jsonhelper.JSONObject)
	if err != nil {
		return []wm.MediaTypeHandler{apiutil.NewJSONMediaTypeHandler(jsonObj, time.Time{}, "")}, req, gpkc, http.StatusInternalServerError, err
	}
	return []wm.MediaTypeHandler{apiutil.NewJSONMediaTypeHandler(jsonObj, time.Time{}, "")}, req, gpkc, 0, nil
}
Beispiel #3
0
func createJSONHttpResponseFromObj(req *http.Request, obj interface{}) (resp *http.Response, err error) {
	var b []byte
	if obj != nil {
		obj1, _ := jsonhelper.MarshalWithOptions(obj, GOOGLE_DATETIME_FORMAT)
		jsonobj := obj1.(jsonhelper.JSONObject)
		b, err = json.Marshal(jsonobj)
	}
	if b == nil {
		b = make([]byte, 0)
	}
	buf := bytes.NewBuffer(b)
	headers := make(http.Header)
	headers.Add("Content-Type", "application/json; charset=utf-8")
	resp = new(http.Response)
	resp.Status = "200 OK"
	resp.StatusCode = http.StatusOK
	resp.Proto = "HTTP/1.1"
	resp.ProtoMajor = 1
	resp.ProtoMinor = 1
	resp.Header = headers
	resp.Body = ioutil.NopCloser(buf)
	resp.ContentLength = int64(len(b))
	resp.TransferEncoding = []string{"identity"}
	resp.Close = true
	resp.Request = req
	return
}
Beispiel #4
0
func (p *UpdateAccountRequestHandler) HandleJSONObjectInputHandler(req wm.Request, cxt wm.Context, inputObj jsonhelper.JSONObject) (int, http.Header, io.WriterTo) {
	uac := cxt.(UpdateAccountContext)
	uac.SetFromJSON(inputObj)
	uac.CleanInput(uac.RequestingUser(), uac.OriginalValue())
	//log.Print("[UARH]: HandleJSONObjectInputHandler()")
	errors := make(map[string][]error)
	var obj interface{}
	var err error
	ds := p.ds
	switch uac.Type() {
	case "user":
		if user := uac.User(); user != nil {
			//log.Printf("[UARH]: user is not nil1: %v\n", user)
			user.Validate(false, errors)
			if len(errors) == 0 {
				user, err = ds.UpdateUserAccount(user)
				//log.Printf("[UARH]: user after errors is %v\n", user)
			}
			obj = user
			uac.SetUser(user)
			//log.Printf("[UARH]: setUser to %v\n", user)
		}
	case "consumer":
		if user := uac.Consumer(); user != nil {
			user.Validate(false, errors)
			if len(errors) == 0 {
				user, err = ds.UpdateConsumerAccount(user)
			}
			obj = user
			uac.SetConsumer(user)
		}
	case "external_user":
		if user := uac.ExternalUser(); user != nil {
			user.Validate(false, errors)
			if len(errors) == 0 {
				user, err = ds.UpdateExternalUserAccount(user)
			}
			obj = user
			uac.SetExternalUser(user)
		}
	default:
		return apiutil.OutputErrorMessage("\"type\" must be \"user\", \"consumer\", or \"external_user\"", nil, 400, nil)
	}
	if len(errors) > 0 {
		return apiutil.OutputErrorMessage("Value errors. See result", errors, http.StatusBadRequest, nil)
	}
	if err != nil {
		return apiutil.OutputErrorMessage(err.Error(), nil, http.StatusInternalServerError, nil)
	}
	theobj, _ := jsonhelper.MarshalWithOptions(obj, dm.UTC_DATETIME_FORMAT)
	jsonObj, _ := theobj.(jsonhelper.JSONObject)
	//log.Printf("[UARH]: obj was: \n%v\n", obj)
	//log.Printf("[UARH]: Going to output:\n%s\n", jsonObj)
	return apiutil.OutputJSONObject(jsonObj, uac.LastModified(), uac.ETag(), http.StatusOK, nil)
}
Beispiel #5
0
func (p *ViewAccountRequestHandler) HandleJSONObjectInputHandler(req wm.Request, cxt wm.Context, inputObj jsonhelper.JSONObject) (int, http.Header, io.WriterTo) {
	vac := cxt.(ViewAccountContext)

	obj := vac.ToObject()
	var err error
	if err != nil {
		return apiutil.OutputErrorMessage(err.Error(), nil, http.StatusInternalServerError, nil)
	}
	theobj, _ := jsonhelper.MarshalWithOptions(obj, dm.UTC_DATETIME_FORMAT)
	jsonObj, _ := theobj.(jsonhelper.JSONObject)
	return apiutil.OutputJSONObject(jsonObj, vac.LastModified(), vac.ETag(), 0, nil)
}
Beispiel #6
0
func (p *ViewAccountRequestHandler) ContentTypesProvided(req wm.Request, cxt wm.Context) ([]wm.MediaTypeHandler, wm.Request, wm.Context, int, error) {
	vac := cxt.(ViewAccountContext)
	obj := vac.ToObject()
	lastModified := vac.LastModified()
	etag := vac.ETag()
	var jsonObj jsonhelper.JSONObject
	if obj != nil {
		theobj, _ := jsonhelper.MarshalWithOptions(obj, dm.UTC_DATETIME_FORMAT)
		jsonObj, _ = theobj.(jsonhelper.JSONObject)
	}
	return []wm.MediaTypeHandler{apiutil.NewJSONMediaTypeHandler(jsonObj, lastModified, etag)}, req, vac, 0, nil
}
Beispiel #7
0
func (p *UpdateContactRequestHandler) HandleJSONObjectInputHandler(req wm.Request, cxt wm.Context, inputObj jsonhelper.JSONObject) (int, http.Header, io.WriterTo) {
	ucc := cxt.(UpdateContactContext)
	ucc.SetFromJSON(inputObj)
	ucc.CleanInput(ucc.AuthUser(), ucc.OriginalContact())
	//log.Print("[UARH]: HandleJSONObjectInputHandler()")
	errors := make(map[string][]error)
	var obj interface{}
	var err error
	contactsDS := p.contactsDS
	contact := ucc.Contact()
	origContact := ucc.OriginalContact()
	if contact != nil && origContact != nil && ucc.User() != nil {
		dsocialUserId := ucc.User().Id
		contact.Id = ucc.ContactId()
		contact.UserId = dsocialUserId
		contact.Validate(false, errors)
		if len(errors) == 0 {
			l := new(list.List)
			origContact.GenerateChanges(origContact, contact, nil, l)
			allowAdd, allowDelete, allowUpdate := true, true, true
			pipeline := bc.NewPipeline()
			l = pipeline.RemoveUnacceptedChanges(l, allowAdd, allowDelete, allowUpdate)
			changes := make([]*dm.Change, l.Len())
			for i, iter := 0, l.Front(); iter != nil; i, iter = i+1, iter.Next() {
				changes[i] = iter.Value.(*dm.Change)
			}
			changeset := &dm.ChangeSet{
				CreatedAt:      time.Now().UTC().Format(dm.UTC_DATETIME_FORMAT),
				ChangedBy:      ucc.AuthUser().Id,
				ChangeImportId: ucc.ContactId(),
				RecordId:       ucc.ContactId(),
				Changes:        changes,
			}
			_, err = contactsDS.StoreContactChangeSet(dsocialUserId, changeset)
			if err == nil {
				contact, err = contactsDS.StoreDsocialContact(contact.UserId, contact.Id, contact)
			}
		}
	}
	if len(errors) > 0 {
		return apiutil.OutputErrorMessage("Value errors. See result", errors, http.StatusBadRequest, nil)
	}
	if err != nil {
		return apiutil.OutputErrorMessage(err.Error(), nil, http.StatusInternalServerError, nil)
	}
	theobj, _ := jsonhelper.MarshalWithOptions(obj, dm.UTC_DATETIME_FORMAT)
	jsonObj, _ := theobj.(jsonhelper.JSONObject)
	return apiutil.OutputJSONObject(jsonObj, ucc.LastModified(), ucc.ETag(), http.StatusOK, nil)
}
Beispiel #8
0
func (p *DeleteAccountRequestHandler) HandleJSONObjectInputHandler(req wm.Request, cxt wm.Context, inputObj jsonhelper.JSONObject) (int, http.Header, io.WriterTo) {
	dac := cxt.(DeleteAccountContext)

	obj := dac.ToObject()
	var err error
	if !dac.Deleted() {
		_, req, cxt, _, err = p.DeleteResource(req, cxt)
	}
	if err != nil {
		return apiutil.OutputErrorMessage(err.Error(), nil, http.StatusInternalServerError, nil)
	}
	theobj, _ := jsonhelper.MarshalWithOptions(obj, dm.UTC_DATETIME_FORMAT)
	jsonObj, _ := theobj.(jsonhelper.JSONObject)
	return apiutil.OutputJSONObject(jsonObj, time.Time{}, "", 0, nil)
}
Beispiel #9
0
func (p *InMemoryDataStore) Decode(r io.Reader) os.Error {
	err := json.NewDecoder(r).Decode(p)
	if err != nil {
		return err
	}
	m := make(map[string]interface{})
	m[_INMEMORY_CONTACT_COLLECTION_NAME] = new(dm.Contact)
	m[_INMEMORY_CONNECTION_COLLECTION_NAME] = new(dm.Contact)
	m[_INMEMORY_GROUP_COLLECTION_NAME] = new(dm.Group)
	m[_INMEMORY_CHANGESET_COLLECTION_NAME] = new(dm.ChangeSet)
	m[_INMEMORY_CONTACT_FOR_EXTERNAL_CONTACT_COLLECTION_NAME] = new(dm.Contact)
	m[_INMEMORY_GROUP_FOR_EXTERNAL_GROUP_COLLECTION_NAME] = new(dm.Group)
	// These three have different types based on the service
	//m[_INMEMORY_EXTERNAL_CONTACT_COLLECTION_NAME] = "external_contacts"
	//m[_INMEMORY_EXTERNAL_GROUP_COLLECTION_NAME] = "external_group"
	//m[_INMEMORY_USER_TO_CONTACT_SETTINGS_COLLECTION_NAME] = "user_to_contact_settings"
	// These four are all strings
	//m[_INMEMORY_EXTERNAL_TO_INTERNAL_CONTACT_MAPPING_COLLECTION_NAME] = ""
	//m[_INMEMORY_INTERNAL_TO_EXTERNAL_CONTACT_MAPPING_COLLECTION_NAME] = ""
	//m[_INMEMORY_EXTERNAL_TO_INTERNAL_GROUP_MAPPING_COLLECTION_NAME] = ""
	//m[_INMEMORY_INTERNAL_TO_EXTERNAL_GROUP_MAPPING_COLLECTION_NAME] = ""
	m[_INMEMORY_CHANGESETS_TO_APPLY_COLLECTION_NAME] = new(dm.ChangeSetsToApply)
	m[_INMEMORY_CHANGESETS_NOT_CURRENTLY_APPLYABLE_COLLECTION_NAME] = new(dm.ChangeSetsToApply)

	for k, collection := range p.Collections {
		for k1, v1 := range collection.Data {
			m1, _ := jsonhelper.MarshalWithOptions(v1, dm.UTC_DATETIME_FORMAT)
			b1, _ := json.Marshal(m1)
			if obj, ok := m[k]; ok {
				err = json.Unmarshal(b1, obj)
				if err != nil {
					return err
				}
				collection.Data[k1] = obj
			} else {
				collection.Data[k1] = b1
			}
		}
	}
	return nil
}
Beispiel #10
0
func (p *CreateAccountRequestHandler) HandleJSONObjectInputHandler(req wm.Request, cxt wm.Context, writer io.Writer, inputObj jsonhelper.JSONObject) (int, http.Header, os.Error) {
	cac := cxt.(CreateAccountContext)
	cac.SetFromJSON(inputObj)
	cac.CleanInput(cac.RequestingUser())

	errors := make(map[string][]os.Error)
	var obj interface{}
	var err os.Error
	ds := p.ds
	if user := cac.User(); user != nil {
		user.Validate(true, errors)
		if len(errors) == 0 {
			user, err = ds.CreateUserAccount(user)
		}
		obj = user
	} else if user := cac.Consumer(); user != nil {
		user.Validate(true, errors)
		if len(errors) == 0 {
			user, err = ds.CreateConsumerAccount(user)
		}
		obj = user
	} else if user := cac.ExternalUser(); user != nil {
		user.Validate(true, errors)
		if len(errors) == 0 {
			user, err = ds.CreateExternalUserAccount(user)
		}
		obj = user
	} else {
		return apiutil.OutputErrorMessage(writer, "\"type\" must be \"user\", \"consumer\", or \"external_user\"", nil, 400, nil)
	}
	if len(errors) > 0 {
		return apiutil.OutputErrorMessage(writer, "Value errors. See result", errors, http.StatusBadRequest, nil)
	}
	if err != nil {
		return apiutil.OutputErrorMessage(writer, err.String(), nil, http.StatusInternalServerError, nil)
	}
	theobj, _ := jsonhelper.MarshalWithOptions(obj, dm.UTC_DATETIME_FORMAT)
	jsonObj, _ := theobj.(jsonhelper.JSONObject)
	return apiutil.OutputJSONObject(writer, jsonObj, cac.LastModified(), cac.ETag(), 0, nil)
}
Beispiel #11
0
func (p *CreateAccountRequestHandler) HandleJSONObjectInputHandler(req wm.Request, cxt wm.Context, inputObj jsonhelper.JSONObject) (int, http.Header, io.WriterTo) {
	cac := cxt.(CreateAccountContext)
	cac.SetFromJSON(inputObj)
	cac.CleanInput(cac.RequestingUser())

	errors := make(map[string][]error)
	var obj map[string]interface{}
	var accessKey *dm.AccessKey
	var err error
	ds := p.ds
	authDS := p.authDS
	if user := cac.User(); user != nil {
		var userPassword *dm.UserPassword
		user.Validate(true, errors)
		if len(errors) == 0 {
			user, err = ds.CreateUserAccount(user)
			if err == nil && user != nil {
				accessKey, err = authDS.StoreAccessKey(dm.NewAccessKey(user.Id, ""))
			}
		}
		if cac.Password() != "" && user != nil && user.Id != "" {
			userPassword = dm.NewUserPassword(user.Id, cac.Password())
			userPassword.Validate(true, errors)
			if len(errors) == 0 && err == nil {
				userPassword, err = authDS.StoreUserPassword(userPassword)
			}
		}
		obj = make(map[string]interface{})
		obj["user"] = user
		obj["type"] = "user"
		obj["key"] = accessKey
	} else if user := cac.Consumer(); user != nil {
		user.Validate(true, errors)
		if len(errors) == 0 {
			user, err = ds.CreateConsumerAccount(user)
			if err == nil && user != nil {
				accessKey, err = authDS.StoreAccessKey(dm.NewAccessKey("", user.Id))
			}
		}
		obj = make(map[string]interface{})
		obj["consumer"] = user
		obj["type"] = "consumer"
		obj["key"] = accessKey
	} else if user := cac.ExternalUser(); user != nil {
		user.Validate(true, errors)
		if len(errors) == 0 {
			user, err = ds.CreateExternalUserAccount(user)
			if err == nil && user != nil {
				accessKey, err = authDS.StoreAccessKey(dm.NewAccessKey(user.Id, user.ConsumerId))
			}
		}
		obj = make(map[string]interface{})
		obj["external_user"] = user
		obj["type"] = "external_user"
		obj["key"] = accessKey
	} else {
		return apiutil.OutputErrorMessage("\"type\" must be \"user\", \"consumer\", or \"external_user\"", nil, 400, nil)
	}
	if len(errors) > 0 {
		return apiutil.OutputErrorMessage("Value errors. See result", errors, http.StatusBadRequest, nil)
	}
	if err != nil {
		return apiutil.OutputErrorMessage(err.Error(), nil, http.StatusInternalServerError, nil)
	}
	theobj, _ := jsonhelper.MarshalWithOptions(obj, dm.UTC_DATETIME_FORMAT)
	jsonObj, _ := theobj.(jsonhelper.JSONObject)
	return apiutil.OutputJSONObject(jsonObj, cac.LastModified(), cac.ETag(), 0, nil)
}